我想将多个值传递给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
现在,如果我传递一个值,那么它可以毫无问题地工作 如果我尝试通过终端传递值列表,它也可以工作,但如果我使用POST将值发送到Serializer,我会收到错误。
#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 (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'
答案 0 :(得分:1)
看起来我遇到了同样的问题,只有当验证失败并且我尝试返回serializer.errors时才会发生这种情况。
答案 1 :(得分:0)
在保存之前,请致电serializer.save()
或使用serializer.validated_data
来访问数据。 serializer.data
属性仅在将实例保存到序列化程序时才有效。
请在此处找到更多信息: https://github.com/encode/django-rest-framework/issues/2964