You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add Song resources to API and update documentation
This commit adds a new resource, Song, to the current music API. Changes include registering the new SongViewSet to the router in urls.py, adding SongSerializer in serializers.py and updating the views in views.py. The getString method for Song model in models.py has also been updated. Additionally, the documentation (index.md) has been updated to reflect these changes.
Copy file name to clipboardexpand all lines: docs/index.md
+48-1
Original file line number
Diff line number
Diff line change
@@ -480,7 +480,7 @@ class AlbumSerializer(serializers.Serializer):
480
480
2. With the fields `title`, `artist`, `release_year` with it respective fields
481
481
3. We also need to list the fields inside the class Meta
482
482
483
-
Additionally we need to add 2 methods to let DRF know how to save and how to update the Albums.
483
+
Additionally, we need to add 2 methods to let DRF know how to save and how to update the Albums.
484
484
485
485
The first is `create`
486
486
@@ -518,6 +518,53 @@ Now we have 2 resources here, artists and albums
518
518
519
519
I hope that at this time you understand the amount of shortcuts DRF gives you at the same time, if you want to customize it, it's still possible.
520
520
521
+
## Building an API - Part III
522
+
### Easy version
523
+
We are going to start from the urls one more time. We will add the route for the songs in our `music.urls` like in the snippet below
524
+
```python
525
+
from django.urls import path, include
526
+
from rest_framework import routers
527
+
528
+
from .views import ArtistViewSet, AlbumViewSet, SongViewSet
529
+
530
+
router = routers.DefaultRouter()
531
+
router.register(r'artists', ArtistViewSet)
532
+
router.register(r'albums', AlbumViewSet)
533
+
# Add this new line below
534
+
router.register(r'songs', SongViewSet)
535
+
536
+
urlpatterns = [
537
+
path('', include(router.urls)),
538
+
# path('', views.index, name='index'),
539
+
]
540
+
```
541
+
After this we are going to create the `SongViewSet` in our `music.views` file using a `ModelViewSet` like in the snippet below. Also update Don't forget to add the import for the `SongSerializer` in your imports
542
+
543
+
```python
544
+
from music.serializers import ArtistSerializer, AlbumSerializer, SongSerializer
545
+
546
+
classSongViewSet(viewsets.ModelViewSet):
547
+
queryset = Song.objects.all()
548
+
serializer_class = SongSerializer
549
+
```
550
+
551
+
At this point the `SongSerializer` doesn't exist yet, so now we are going to create it. Here also update the imports for the models including the `Song` model. Here there is a snippet so you see the changes you have to do on you `music.serializers` file.
With this part done you will be able to run you application and see something like this on you api with all 3 resource working in your API, Artist, Album and Song. 🥳
0 commit comments