Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does pop()' do in Django Rest FrameWork?

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
like image 547
Thomas Saied Avatar asked May 16 '19 21:05

Thomas Saied


People also ask

Why do we need Serializers in Django REST framework?

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.

What is validated_data?

validated_data is an OrderedDict and you can see it only after is_valid() and is_valid() == True.

What does serializer save () do?

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.

How do you pass extra context data to Serializers in Django REST framework?

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.

What is Django REST framework?

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 in Django?

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:

What is the use of request stream in Django?

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.

What is the use of REST framework?

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.


1 Answers

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.

like image 62
Jonatan Avatar answered Nov 15 '22 07:11

Jonatan