Commit f2a1ab5 1 parent c031890 commit f2a1ab5 Copy full SHA for f2a1ab5
File tree 2 files changed +64
-4
lines changed
2 files changed +64
-4
lines changed Original file line number Diff line number Diff line change @@ -111,9 +111,47 @@ This will create the app structure for us. Something similar to this below:
111
111
112
112
Now the next step is create the models we are going to use in our API to represent the domain models.
113
113
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
+ ```
117
155
118
156
## URL Mapping and Views
119
157
Original file line number Diff line number Diff line change 1
1
from django .db import models
2
2
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
You can’t perform that action at this time.
0 commit comments