from .models import Book
class PublisherSerializer(serializers.Serializer):
id = serializers.IntegerField(read_only=True)
title = serializers.CharField(max_length=32)
class UserSerializer(serializers.Serializer):
id = serializers.IntegerField(read_only=True)
name = serializers.CharField(max_length=32)
age = serializers.IntegerField()
class BookSerializer(serializers.Serializer):
id = serializers.IntegerField(read_only=True)
title = serializers.CharField(max_length=32)
CHOICES = ((1,'Linux'),(2,'Python'),(3,'Go'))
category= serializers.ChoiceField(choices=CHOICES,
source='get_category_display',read_only=True)
pub_time = serializers.DateField()
publisher = PublisherSerializer(read_only=True)
user = UserSerializer(many=True,read_only=True)
id = serializers.IntegerField(read_only=True)
title = serializers.CharField(max_length=32)
CHOICES = ((1,'Linux'),(2,'Python'),(3,'Go'))
category = serializers.ChoiceField(choices=CHOICES,
source='get_category_display',read_only=True)
post_category = serializers.IntegerField(write_only=True)
pub_time = serializers.DateField()
publisher = PublisherSerializer(read_only=True)
user = UserSerializer(many=True, read_only=True)
users = serializers.ListField(write_only=True)
publisher_id = serializers.IntegerField(write_only=True)
def create(self, validated_date):
book = Book.objects.create(title=validated_data['title'],
category=validated_data['post_category'], pub_time=validate_data['pub_time'],
publisher_id=validated_data['publisher_id'])
book.user.add(*validated_data['users'])
return book