Skip to content

Commit

Permalink
Implement AlbumSerializer in music app and update docs
Browse files Browse the repository at this point in the history
Expanded the existing service of the music app by introducing the AlbumSerializer. This includes defining required fields, and creating methods for 'create' and 'update' operations. The documentation is correspondingly updated to explain the new AlbumSerializer and its methods.
  • Loading branch information
lipemorais committed Apr 28, 2024
1 parent eb7fa0f commit fb54dfc
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
48 changes: 48 additions & 0 deletions docs/images/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,54 @@ Last but not least `destroy` action to delete albums

With `destroy` set the we finish the change on `music.views.py`

Now we go for our `AlbumSerializer`

```python
class AlbumSerializer(serializers.Serializer):
title = serializers.CharField()
artist = ArtistSerializer() # Serializers inherits from Field, so it can be used as fields too
release_year = serializers.IntegerField()

class Meta:
fields = ['title', 'artist', 'release_year']
```

1. First we define our `AlbumSerializer` inheriting from `serializers.Serializer`
2. With the fields `title`, `artist`, `release_year` with it respective fields
3. We also need to list the fields inside the class Meta

Additionally we need to add 2 methods to let DRF know how to save and how to update the Albums.

The first is `create`

```python
def create(self, validated_data):
artist_data = validated_data.pop('artist')
artist, created = Artist.objects.get_or_create(name=artist_data['name'])
return Album.objects.create(artist=artist, **validated_data)
```
1. Where we create the album but since the album has artist as a nested model we need to create it here before try to save the album itself.
2. Don't forget to import the `Album` model here with `from music.models import Album`

and the `update` method
```python
def update(self, album, validated_data):
artist_data = validated_data.pop('artist')
artist, created = Artist.objects.get_or_create(name=artist_data['name'])

album.title = validated_data.get('title', album.title)
album.release_year = validated_data.get('release_year', album.release_year)
album.artist = artist
album.save()

return album
```
1. To update we need to do the same with the `Artist` model, creating/getting it before save the Album
2. So we get the new fields and save it
3. Last we return the updated album

With everything set we can see it working live.

## Bonus content

### Serializers deep dive
Expand Down
30 changes: 29 additions & 1 deletion first_api/music/serializers.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,37 @@
from rest_framework import serializers

from music.models import Artist
from music.models import Artist, Album


class ArtistSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Artist
fields = ['name']


# You could use the HyperlinkedModelSerializer here but
# want you to know how a plain serializer works
# class AlbumSerializer(serializers.HyperlinkedModelSerializer):
class AlbumSerializer(serializers.Serializer):
title = serializers.CharField()
artist = ArtistSerializer() # Serializers inherits from Field, so it can be used as fields too
release_year = serializers.IntegerField()

class Meta:
fields = ['title', 'artist', 'release_year']

def create(self, validated_data):
artist_data = validated_data.pop('artist')
artist, created = Artist.objects.get_or_create(name=artist_data['name'])
return Album.objects.create(artist=artist, **validated_data)

def update(self, album, validated_data):
artist_data = validated_data.pop('artist')
artist, created = Artist.objects.get_or_create(name=artist_data['name'])

album.title = validated_data.get('title', album.title)
album.release_year = validated_data.get('release_year', album.release_year)
album.artist = artist
album.save()

return album

0 comments on commit fb54dfc

Please sign in to comment.