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',)
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".
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.
it validates your serializer with the condition of respective field specified in your serializer MessageSerializer .
— 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.
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.
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."
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.
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.
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