Skip to content

Commit

Permalink
Add Artist, Album, Song models and update documentation
Browse files Browse the repository at this point in the history
Models for Artist, Album, and Song have been included in the Django application inside the music app. The models were designed to carry out the required relationships in the music domain. Updated documentation to reflect the new additions and provided code examples for better understanding.
  • Loading branch information
lipemorais committed Apr 18, 2024
1 parent c031890 commit f2a1ab5
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 4 deletions.
44 changes: 41 additions & 3 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,47 @@ This will create the app structure for us. Something similar to this below:

Now the next step is create the models we are going to use in our API to represent the domain models.

- 🎬 Lecture : Fundamentals of Django models.
- 💻 Exercise : Hands-on creation and manipulation of models.
- 💡 Purpose: Understanding data handling in Django.
We are going to create 3 models in the file models.py

1. Artist
2. Album
3. Song

Let's start with the artist model.

```python
class Artist(models.Model):
name = models.CharField(max_length=100)

def __str__(self):
return self.name
```

Don't forget to import the models
```python
from django.db import models
```
Now the album model
```python
class Album(models.Model):
title = models.CharField(max_length=100)
artist = models.ForeignKey(Artist, on_delete=models.CASCADE)
release_year = models.IntegerField()

def __str__(self):
return self.title
```

the last model will be the song model that will have relationship with artist and album.

```python
class Song(models.Model):
author = models.CharField(max_length=100)
title = models.CharField(max_length=100)
artist = models.ForeignKey(Artist, on_delete=models.CASCADE) # Artist or band name
album = models.ForeignKey(Album, on_delete=models.CASCADE) # Album the song belongs to
duration = models.IntegerField() # Duration of the song in seconds
```

## URL Mapping and Views

Expand Down
24 changes: 23 additions & 1 deletion first_api/music/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
from django.db import models

# Create your models here.

class Artist(models.Model):
name = models.CharField(max_length=100)

def __str__(self):
return self.name


class Album(models.Model):
title = models.CharField(max_length=100)
artist = models.ForeignKey(Artist, on_delete=models.CASCADE)
release_year = models.IntegerField()

def __str__(self):
return self.title


class Song(models.Model):
author = models.CharField(max_length=100)
title = models.CharField(max_length=100)
artist = models.ForeignKey(Artist, on_delete=models.CASCADE) # Artist or band name
album = models.ForeignKey(Album, on_delete=models.CASCADE) # Album the song belongs to
duration = models.IntegerField() # Duration of the song in seconds

0 comments on commit f2a1ab5

Please sign in to comment.