Created
September 7, 2022 10:46
-
-
Save DJWOMS/1e33dd6e8221da463eefdeee913c9eea to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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