5.Serializer,ModelSerializer区别

Serializer示例代码:

# 使用Serializer
class BookSerializer(serializers.Serializer):
    id = serializers.IntegerField(read_only=True)
    title = serializers.CharField(max_length=100)
    author = serializers.CharField(max_length=100)
    publication_date = serializers.DateField()
 
    def create(self, validated_data):
        return Book.objects.create(**validated_data)
 
    def update(self, instance, validated_data):
        instance.title = validated_data.get('title', instance.title)
        instance.author = validated_data.get('author', instance.author)
        instance.publication_date = validated_data.get('publication_date', instance.publication_date)
        instance.save()
        return instance

ModelSerializer示例代码:

 
 

你可能感兴趣的:(python,后端,开发语言)