Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to use my DRF serializers manually

Very strange but whenever I try to use any of my DRF serializers to serialize an object, like for instance:

me = CustomUser.objects.all()[0]
serializer = CustomUserSerializer(me)

serializer.is_valid()
# --> False
print(serializer.errors)
# {"non_field_errors": ["No input provided"]}

and this happens with totally different serializers and various objects.

However, if I use a class-based view (that indirectly uses the same serializers and the same objects), I am able to receive a JSON response with the data serialized as expected. Said differently, calling an endpoint linked to this view

class CustomUserList(generics.ListAPIView):
    queryset = CustomUser.objects.all()
    serializer_class = CustomUserSerializer

will indeed return a JSON representation of all the CustomUsers in the database.

There must be something I did not quite understand.

like image 837
Buddyshot Avatar asked Dec 09 '14 14:12

Buddyshot


People also ask

How do I know if my serializer is valid?

We can validate the serializer by calling the method " is_valid() ". It will return the boolean(True/False) value. If the serializer is not valid then we can get errors by using the attribute "errors".

Is it necessary to use Serializers in Django?

It is not necessary to use a serializer. You can do what you would like to achieve in a view. However, serializers help you a lot. If you don't want to use serializer, you can inherit APIView at a function-based-view.

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 the use of Serializers?

Serializers allow complex data such as querysets and model instances to be converted to native Python datatypes that can then be easily rendered into JSON , XML or other content types.

How do serializers integrate well with DRFs?

Django automatically includes all model fields in the serializer and creates the create and update methods. Like Forms with Django's CBVs, serializers integrate well with DRFs.

How to add a serializer to a DRF in Django?

Django automatically includes all model fields in the serializer and creates the create and update methods. Like Forms with Django's CBVs, serializers integrate well with DRFs. You can set the serializer_class attribute so that the serializer will be available to the view:

How do I serialize a user with all groups in drfish?

A DRFish way is to add an all_groups field on the serializer and set it to GroupSerializer. Let’s serialize a user and verify that the associated group’s information is present in serialized data. DRF is smart enough to call user.groups.all (), even though we just set source=groups.

What is the use of serializer in rest?

Serializers also provide deserialization, allowing parsed data to be converted back into complex types, after first validating the incoming data. The serializers in REST framework work very similarly to Django’s Form and ModelForm classes.


Video Answer


1 Answers

Django REST Framework allows you to serialize an object by passing it into a serializer through the instance keyword (or the first positional argument). From there, you just need to call data on it. This is all covered in the serializing objects part of the documentation.

me = CustomUser.objects.all()[0]
serializer = CustomUserSerializer(me)
serializer.data

You only need to call is_valid when you are deserializing data into an object. The error you are getting ("No input provided") is because you are trying to validate the data to be deserialized, but you are passing no data in.

like image 81
Kevin Brown-Silva Avatar answered Oct 12 '22 23:10

Kevin Brown-Silva