Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

django rest framework serializers

#
class CommentSerializer(serializers.Serializer):
    email = serializers.EmailField()
    content = serializers.CharField(max_length=200)
    created = serializers.DateTimeField()
    def create(self, validated_data):
        return Comment.objects.create(**validated_data)
		# or return Comment(**validated_data)

    def update(self, instance, validated_data):
        instance.email = validated_data.get('email', instance.email)
        instance.content = validated_data.get('content', instance.content)
        instance.created = validated_data.get('created', instance.created)
        instance.save()
        return instance
#Calling .save() will either create a new instance, or update an existing instance, depending on if an existing instance was passed when instantiating the serializer class:
# .save() will create a new instance.
serializer = CommentSerializer(data=data)

# .save() will update the existing `comment` instance.
serializer = CommentSerializer(comment, data=data)


#serializer errors
from rest_framework import serializers
if --- :
  raise serializers.ValidationError("Blog post is not about Django")

Comment

PREVIOUS NEXT
Code Example
Python :: formatted string in python 
Python :: example of tinker in python 
Python :: guardar plot python 
Python :: django debug toolbar urlpatterns 
Python :: how to append data in excel using python 
Python :: python draw tree 
Python :: create jwt token in django 
Python :: python dictionary contains key 
Python :: pandas count occurrences of certain value in row 
Python :: compound interest python 
Python :: python dataframe contains value 
Python :: python problem append same value 
Python :: tqdm spamming 
Python :: minio python check if bucket exists 
Python :: tkinter how to update optionmenu contents 
Python :: dockerize django app 
Python :: telegram.ext python 
Python :: webdriver python get total number of tabs 
Python :: full row visible in jupyter notebook 
Python :: fibonacci numbers in reverse order 
Python :: enumerate() 
Python :: snakeviz python profile 
Python :: Resource stopwords not found 
Python :: python remove vowels from string 
Python :: Python Global in Nested Functions 
Python :: install python modules without pip 
Python :: activate python venv in windows 
Python :: how to instal django cities 
Python :: reverse order of dataframe rows 
Python :: NumPy fliplr Syntax 
ADD CONTENT
Topic
Content
Source link
Name
3+3 =