Skip to content

Commit

Permalink
serializers changed back into serializers.Serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexandr-Politov committed Oct 1, 2024
1 parent 8a4b123 commit 008cfb6
Showing 1 changed file with 75 additions and 16 deletions.
91 changes: 75 additions & 16 deletions cinema/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,91 @@
from cinema.models import Actor, Genre, CinemaHall, Movie


class ActorSerializer(serializers.ModelSerializer):
class Meta:
model = Actor
fields = ("id", "first_name", "last_name")
class ActorSerializer(serializers.Serializer):
id = serializers.IntegerField(read_only=True)
first_name = serializers.CharField(max_length=50)
last_name = serializers.CharField(max_length=50)

def create(self, validated_data):
return Actor.objects.create(**validated_data)

class GenreSerializer(serializers.ModelSerializer):
class Meta:
model = Genre
fields = ("id", "name",)
def update(self, instance, validated_data):
instance.first_name = validated_data.get(
"first_name", instance.first_name
)
instance.last_name = validated_data.get(
"last_name", instance.last_name
)
instance.save()
return instance


class CinemaHallSerializer(serializers.ModelSerializer):
class Meta:
model = CinemaHall
fields = ("id", "name", "rows", "seats_in_row")
class GenreSerializer(serializers.Serializer):
id = serializers.IntegerField(read_only=True)
name = serializers.CharField(
max_length=50,
validators=[UniqueValidator(queryset=Genre.objects.all())]
)

def create(self, validated_data):
return Genre.objects.create(**validated_data)

def update(self, instance, validated_data):
instance.name = validated_data.get("name", instance.name)
instance.save()
return instance


class CinemaHallSerializer(serializers.Serializer):
id = serializers.IntegerField(read_only=True)
name = serializers.CharField(max_length=50)
rows = serializers.IntegerField()
seats_in_row = serializers.IntegerField()

class MovieSerializer(serializers.ModelSerializer):
def create(self, validated_data):
return CinemaHall.objects.create(**validated_data)

def update(self, instance, validated_data):
instance.name = validated_data.get("name", instance.name)
instance.rows = validated_data.get("rows", instance.rows)
instance.seats_in_row = validated_data.get(
"seats_in_row", instance.seats_in_row
)
instance.save()
return instance


class MovieSerializer(serializers.Serializer):
id = serializers.IntegerField(read_only=True)
title = serializers.CharField(max_length=255)
description = serializers.CharField()
duration = serializers.IntegerField()
actors = serializers.PrimaryKeyRelatedField(
many=True, queryset=Actor.objects.all()
)
genres = serializers.PrimaryKeyRelatedField(
many=True, queryset=Genre.objects.all()
)

class Meta:
model = Movie
fields = ("id", "title", "description", "actors", "genres", "duration")
def create(self, validated_data):
actors = validated_data.pop("actors")
genres = validated_data.pop("genres")
movie = Movie.objects.create(**validated_data)
movie.actors.set(actors)
movie.genres.set(genres)
return movie

def update(self, instance, validated_data):
instance.title = validated_data.get("title", instance.title)
instance.description = validated_data.get(
"description", instance.description
)
instance.duration = validated_data.get("duration", instance.duration)
if "actors" in validated_data:
actors = validated_data.pop("actors")
instance.actors.set(actors)
if "genres" in validated_data:
genres = validated_data.pop("genres")
instance.genres.set(genres)
instance.save()
return instance

0 comments on commit 008cfb6

Please sign in to comment.