Skip to content

Commit eb7fa0f

Browse files
committed
Add CRUD operations to AlbumViewSet
Implemented list, create, retrieve, update, partial_update and destroy operations for the AlbumViewSet in music views. Imported necessary libraries and wrote code to handle these operations, adding functionality to handle album management in the music application. Also, updated the documentation to detail these changes and usage.
1 parent 6383c61 commit eb7fa0f

File tree

2 files changed

+171
-3
lines changed

2 files changed

+171
-3
lines changed

docs/images/index.md

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,133 @@ urlpatterns = [
337337
```
338338
1. We added a new import for the AlbumViewSet
339339
2. We added the routes for albums
340+
341+
Next is the `AlbumViewSet` in the `music.views.py`. I will break it down in some steps.
342+
343+
```python
344+
class AlbumViewSet(viewsets.ViewSet):
345+
queryset = Album.objects.all()
346+
serializer_class = AlbumSerializer
347+
```
348+
1. Here we create a class `AlbumViewSet` inheriting from `views.ViewSet`, pay attention, this is nos a model view set.
349+
2. Set the `queryset`
350+
3. Set `serializer_class`, we are going to talk about this `AlbumSerializer` later
351+
352+
After this still in the same view we are going through what DRF call actions.
353+
Instead of have methods in the view set for get, post, delete... It has functions based on actions. The actions are the ones below:
354+
355+
```python
356+
class AlbumViewSet(viewsets.ViewSet):
357+
queryset = Album.objects.all()
358+
serializer_class = AlbumSerializer
359+
360+
def list(self, request):
361+
"""
362+
List the resources, albums in this case
363+
"""
364+
pass
365+
366+
def create(self, request):
367+
"""
368+
List the resources, albums in this case
369+
"""
370+
pass
371+
372+
def retrieve(self, request, pk=None):
373+
"""
374+
Retrieve a single resources, album in this case
375+
"""
376+
pass
377+
378+
def update(self, request, pk=None):
379+
"""
380+
Update the resource, album in this case
381+
"""
382+
pass
383+
384+
def partial_update(self, request, pk=None):
385+
"""
386+
Partially update the resource, album in this case
387+
"""
388+
pass
389+
390+
def destroy(self, request, pk=None):
391+
"""
392+
Delete the resource, album in this case
393+
"""
394+
pass
395+
```
396+
397+
With the actions in place we will fill each of these methods.
398+
First the `list` method.
399+
400+
```python
401+
def list(self, request):
402+
serializer = self.serializer_class(self.queryset, many=True)
403+
return Response(serializer.data)
404+
```
405+
1. Here we just need to serialize the queryset and return it as a
406+
2. Don't forget to import the `from rest_framework.response import Response`
407+
408+
The following action will be `create`
409+
410+
```python
411+
def create(self, request):
412+
serializer = self.serializer_class(data=request.data)
413+
serializer.is_valid(raise_exception=True)
414+
serializer.save()
415+
return Response(serializer.data, status=status.HTTP_201_CREATED)
416+
```
417+
1. Here we need to serialize the `request.data`
418+
2. Check if it `is_valid`
419+
3. Save our Album(There is caveat here I will show later how to fix)
420+
4. And return a response with the `serializer.data`a and a https status
421+
5. Don't forget to import `from rest_framework import status`
422+
423+
Next is `retrieve`
424+
```python
425+
def retrieve(self, request, pk=None):
426+
album = get_object_or_404(Album, pk=pk)
427+
serializer = self.serializer_class(album)
428+
return Response(serializer.data)
429+
```
430+
1. We try to get the album using the `get_object_or_404` from `from rest_framework.generics import get_object_or_404`
431+
2. serialize it and send it back as a response
432+
433+
So we have the `update`
434+
```python
435+
def update(self, request, pk=None):
436+
album = get_object_or_404(Album, pk=pk)
437+
serializer = self.serializer_class(album, data=request.data)
438+
serializer.is_valid(raise_exception=True)
439+
serializer.save()
440+
return Response(serializer.data)
441+
```
442+
1. Similarly to the previous one but this time we update just the needed fields
443+
444+
For `partial_update` is close to the one above
445+
```python
446+
def partial_update(self, request, pk=None):
447+
album = get_object_or_404(Album, pk=pk)
448+
serializer = self.serializer_class(album, data=request.data, partial=True)
449+
serializer.is_valid(raise_exception=True)
450+
serializer.save()
451+
return Response(serializer.data)
452+
```
453+
1. the only difference here from the `update` above is the `partial=True` so it know that it just need to update some fields not all of them.
454+
455+
Last but not least `destroy` action to delete albums
456+
```python
457+
def destroy(self, request, pk=None):
458+
album = get_object_or_404(Album, pk=pk)
459+
album.delete()
460+
return Response(status=status.HTTP_204_NO_CONTENT)
461+
```
462+
1. Here we need to get the album and call the `.delete()` on it
463+
464+
465+
With `destroy` set the we finish the change on `music.views.py`
466+
340467
## Bonus content
341468

342469
### Serializers deep dive

first_api/music/views.py

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
from django.http import HttpResponse
2-
from rest_framework import viewsets
2+
from rest_framework import viewsets, status
3+
from rest_framework.generics import get_object_or_404
4+
from rest_framework.response import Response
35

4-
from music.models import Artist
5-
from music.serializers import ArtistSerializer
6+
from music.models import Artist, Album
7+
from music.serializers import ArtistSerializer, AlbumSerializer
68

79

810
def index(_request):
@@ -12,3 +14,42 @@ def index(_request):
1214
class ArtistViewSet(viewsets.ModelViewSet):
1315
queryset = Artist.objects.all()
1416
serializer_class = ArtistSerializer
17+
18+
19+
class AlbumViewSet(viewsets.ViewSet):
20+
queryset = Album.objects.all()
21+
serializer_class = AlbumSerializer
22+
23+
def list(self, request):
24+
serializer = self.serializer_class(self.queryset, many=True)
25+
return Response(serializer.data)
26+
27+
def create(self, request):
28+
serializer = self.serializer_class(data=request.data)
29+
serializer.is_valid(raise_exception=True)
30+
serializer.save()
31+
return Response(serializer.data, status=status.HTTP_201_CREATED)
32+
33+
def retrieve(self, request, pk=None):
34+
album = get_object_or_404(Album, pk=pk)
35+
serializer = self.serializer_class(album)
36+
return Response(serializer.data)
37+
38+
def update(self, request, pk=None):
39+
album = get_object_or_404(Album, pk=pk)
40+
serializer = self.serializer_class(album, data=request.data)
41+
serializer.is_valid(raise_exception=True)
42+
serializer.save()
43+
return Response(serializer.data)
44+
45+
def partial_update(self, request, pk=None):
46+
album = get_object_or_404(Album, pk=pk)
47+
serializer = self.serializer_class(album, data=request.data, partial=True)
48+
serializer.is_valid(raise_exception=True)
49+
serializer.save()
50+
return Response(serializer.data)
51+
52+
def destroy(self, request, pk=None):
53+
album = get_object_or_404(Album, pk=pk)
54+
album.delete()
55+
return Response(status=status.HTTP_204_NO_CONTENT)

0 commit comments

Comments
 (0)