Skip to content

Commit f2a1ab5

Browse files
committed
Add Artist, Album, Song models and update documentation
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.
1 parent c031890 commit f2a1ab5

File tree

2 files changed

+64
-4
lines changed

2 files changed

+64
-4
lines changed

docs/index.md

+41-3
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,47 @@ This will create the app structure for us. Something similar to this below:
111111

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

114-
- 🎬 Lecture : Fundamentals of Django models.
115-
- 💻 Exercise : Hands-on creation and manipulation of models.
116-
- 💡 Purpose: Understanding data handling in Django.
114+
We are going to create 3 models in the file models.py
115+
116+
1. Artist
117+
2. Album
118+
3. Song
119+
120+
Let's start with the artist model.
121+
122+
```python
123+
class Artist(models.Model):
124+
name = models.CharField(max_length=100)
125+
126+
def __str__(self):
127+
return self.name
128+
```
129+
130+
Don't forget to import the models
131+
```python
132+
from django.db import models
133+
```
134+
Now the album model
135+
```python
136+
class Album(models.Model):
137+
title = models.CharField(max_length=100)
138+
artist = models.ForeignKey(Artist, on_delete=models.CASCADE)
139+
release_year = models.IntegerField()
140+
141+
def __str__(self):
142+
return self.title
143+
```
144+
145+
the last model will be the song model that will have relationship with artist and album.
146+
147+
```python
148+
class Song(models.Model):
149+
author = models.CharField(max_length=100)
150+
title = models.CharField(max_length=100)
151+
artist = models.ForeignKey(Artist, on_delete=models.CASCADE) # Artist or band name
152+
album = models.ForeignKey(Album, on_delete=models.CASCADE) # Album the song belongs to
153+
duration = models.IntegerField() # Duration of the song in seconds
154+
```
117155

118156
## URL Mapping and Views
119157

first_api/music/models.py

+23-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
11
from django.db import models
22

3-
# Create your models here.
3+
4+
class Artist(models.Model):
5+
name = models.CharField(max_length=100)
6+
7+
def __str__(self):
8+
return self.name
9+
10+
11+
class Album(models.Model):
12+
title = models.CharField(max_length=100)
13+
artist = models.ForeignKey(Artist, on_delete=models.CASCADE)
14+
release_year = models.IntegerField()
15+
16+
def __str__(self):
17+
return self.title
18+
19+
20+
class Song(models.Model):
21+
author = models.CharField(max_length=100)
22+
title = models.CharField(max_length=100)
23+
artist = models.ForeignKey(Artist, on_delete=models.CASCADE) # Artist or band name
24+
album = models.ForeignKey(Album, on_delete=models.CASCADE) # Album the song belongs to
25+
duration = models.IntegerField() # Duration of the song in seconds

0 commit comments

Comments
 (0)