Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

djangorestframework serializer errors: {u'non_field_errors': [u'No input provided']}

I'm using djangorestframework, and someone makes a PUT request to a .../peoplelist/2/markAsSeen, passing in just a Person object's id in the URL. I fetch the Person object (2 in this case) and then simply change the fetched Person object's field has_been_viewed to True. The updated Person object is then to be serialized and returned back to the client.

if request.method == 'PUT':
    serializer = PersonSerializer(person,partial=True)#person is a valid object here
    if serializer.is_valid():
        serializer.save()
        return Response(serializer.data)
    else:
        return Response(serializer.errors,)

serializer errors is {u'non_field_errors': [u'No input provided']} serializer.data looks fine to me

class PersonSerializer(serializers.ModelSerializer):

    class Meta:
        model = Person
        fields = ('id',)
like image 686
user798719 Avatar asked Dec 17 '13 07:12

user798719


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".

How do I pass extra data to a 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.

What does serializer Is_valid do?

it validates your serializer with the condition of respective field specified in your serializer MessageSerializer .

What is a serializer field in Django?

— Django documentation Serializer fields handle converting between primitive values and internal datatypes. They also deal with validating input values, as well as retrieving and setting the values from their parent objects.

What is Django-REST-framework-serializer-extensions?

The django-rest-framework-serializer-extensions package provides a collection of tools to DRY up your serializers, by allowing fields to be defined on a per-view/request basis. Fields can be whitelisted, blacklisted and child serializers can be optionally expanded.

Is there a modelserializeroptions option for char fields in Django?

class ModelSerializerOptions is not available in Django-rest-framework release. Note: you might want to set both required and blank for Char fields. From the docs: "Please keep in mind that, if the field has already been explicitly declared on the serializer class, then the extra_kwargs option will be ignored."

What is a serializer_field_mapping in Salesforce?

Called to generate a serializer field that maps to a standard model field. The default implementation returns a serializer class based on the serializer_field_mapping attribute. Called to generate a serializer field that maps to a relational model field.


Video Answer


1 Answers

You're providing a person instance to be updated by the serializer, but no accompanying data to update that instance with.

If you want to deserialize some request data to update that instance with then you're missing the data argument, for example:

PersonSerializer(person, data=request.DATA, partial=True)

However it sounds like the endpoint you want doesn't actually expect to deal with any input data (it's just an empty PUT request you're making right?) In which case you don't want/need to be using a serializer at all.

like image 68
Tom Christie Avatar answered Oct 19 '22 09:10

Tom Christie