I want to pass multiple values to a Serializer.
#models.py
class Attendance(models.Model):
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
attendance_of = GenericForeignKey()
day = models.DateField()
is_present = models.BooleanField(default=True)
remark = models.CharField(max_length=100, blank=True, default='')
#serializers.py
class AttendanceSerializer(serializers.ModelSerializer):
class Meta:
model = Attendance
Now if I pass a single value then it works without any problem.
If I try to pass a List of values via terminal it also works, but if I use POST to send values to Serializer I get an error.
#payload sample
request.data = [
{u'remark': u'OK', u'is_present': u'true', u'day': u'2016-01-31', u'object_id': 1, u'content_type': 19, "id": 1,},
{u'remark': u'OK', u'is_present': u'true', u'day': u'2016-02-31', u'object_id': 1, u'content_type': 19, "id": 2,}
]
Traceback:
Traceback (most recent call last):
File "/home/prime/.virtualenvs/omapi/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 174, in get_response
response = self.process_exception_by_middleware(e, request)
File "/home/prime/.virtualenvs/omapi/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 172, in get_response
response = response.render()
File "/home/prime/.virtualenvs/omapi/local/lib/python2.7/site-packages/django/template/response.py", line 160, in render
self.content = self.rendered_content
File "/home/prime/.virtualenvs/omapi/local/lib/python2.7/site-packages/rest_framework/response.py", line 71, in rendered_content
ret = renderer.render(self.data, media_type, context)
File "/home/prime/.virtualenvs/omapi/local/lib/python2.7/site-packages/rest_framework/renderers.py", line 676, in render
context = self.get_context(data, accepted_media_type, renderer_context)
File "/home/prime/.virtualenvs/omapi/local/lib/python2.7/site-packages/rest_framework/renderers.py", line 651, in get_context
'post_form': self.get_rendered_html_form(data, view, 'POST', request),
File "/home/prime/.virtualenvs/omapi/local/lib/python2.7/site-packages/rest_framework/renderers.py", line 499, in get_rendered_html_form
serializer.data,
File "/home/prime/.virtualenvs/omapi/local/lib/python2.7/site-packages/rest_framework/serializers.py", line 674, in data
ret = super(ListSerializer, self).data
File "/home/prime/.virtualenvs/omapi/local/lib/python2.7/site-packages/rest_framework/serializers.py", line 243, in data
self._data = self.get_initial()
File "/home/prime/.virtualenvs/omapi/local/lib/python2.7/site-packages/rest_framework/serializers.py", line 534, in get_initial
return self.to_representation(self.initial_data)
File "/home/prime/.virtualenvs/omapi/local/lib/python2.7/site-packages/rest_framework/serializers.py", line 614, in to_representation
self.child.to_representation(item) for item in iterable
File "/home/prime/.virtualenvs/omapi/local/lib/python2.7/site-packages/rest_framework/serializers.py", line 472, in to_representation
ret[field.field_name] = field.to_representation(attribute)
File "/home/prime/.virtualenvs/omapi/local/lib/python2.7/site-packages/rest_framework/relations.py", line 231, in to_representation
return value.pk
AttributeError: 'int' object has no attribute 'pk'
Either call serializer.save()
or use serializer.validated_data
in order to access data prior to saving. The serializer.data
property is only valid if you have a saved an instance to the serializer.
Please find more information here: https://github.com/encode/django-rest-framework/issues/2964
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