I want to understand why this function is used for
class UserSerializer(serializers.ModelSerializer): profile = ProfileSerializer()
class Meta:
model = User
fields = ('username', 'email', 'profile')
def create(self, validated_data):
profile_data = validated_data.pop('profile')
user = User.objects.create(**validated_data)
Profile.objects.create(user=user, **profile_data)
return user
Serializers in Django REST Framework are responsible for converting objects into data types understandable by javascript and front-end frameworks. Serializers also provide deserialization, allowing parsed data to be converted back into complex types, after first validating the incoming data.
validated_data is an OrderedDict and you can see it only after is_valid() and is_valid() == True.
serializer. save() calls def save() method in the serializer. save() method in the serializer will call either def update() or def create() methods in the serializer.
In function-based views, we can pass extra context to serializer with “context” parameter with a dictionary. To access the extra context data inside the serializer we can simply access it with “self. context”. From example, to get “exclude_email_list” we just used code 'exclude_email_list = self.
Django Rest Framework – An Introduction. Let’s look at how to create a RESTFul API for our Django Talk Project using Django Rest Framework (DRF), which is an application used for rapidly building RESTful APIs based on Django models.
What is an API? Django Rest Framework lets you create RESTful APIs: A way to transfer information between an interface and a database in a simple way. It separates user interface and data storage and communicates user and database sending a .json file. Just like this one:
request.stream returns a stream representing the content of the request body. You won't typically need to directly access the request's content, as you'll normally rely on REST framework's default request parsing behavior. As REST framework's Request extends Django's HttpRequest, all the other standard attributes and methods are also available.
Django Rest Framework lets you create RESTful APIs: A way to transfer information between an interface and a database in a simple way. It separates user interface and data storage and communicates user and database sending a .json file.
validated_data is a dictionary and .pop(key)
searches for the key specified and returns and removes it if it is found, otherwise an exception is thrown.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With