Skip to content

Instantly share code, notes, and snippets.

@DJWOMS
Created September 7, 2022 10:46
Show Gist options
  • Save DJWOMS/1e33dd6e8221da463eefdeee913c9eea to your computer and use it in GitHub Desktop.
Save DJWOMS/1e33dd6e8221da463eefdeee913c9eea to your computer and use it in GitHub Desktop.
from rest_framework import serializers
class CommentSerializer(serializers.Serializer):
email = serializers.EmailField()
content = serializers.CharField(max_length=200)
created = serializers.DateTimeField()
def restore_object(self, attrs, instance=None):
"""
Учитывая словарь десериализованных значений поля, либо обновить существующий экземпляр модели или
создать новый экземпляр модели.
"""
if instance is not None:
instance.email = attrs.get('email', instance.email)
instance.content = attrs.get('content', instance.content)
instance.created = attrs.get('created', instance.created)
return instance
return Comment(**attrs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment