Django:TypeError:“ ListSerializer”对象不可迭代

时间:2019-10-10 02:07:18

标签: django python-3.x

我正在将多个值发布到我的“ KeyboardEventView” ModelViewSet中。发布成功,我可以看到数据库中的数据已正确记录。但是,我收到了Django抛出的错误,

...
  File "A:\Python\Enviourments\testapplication\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "A:\Python\Enviourments\testapplication\lib\site-packages\django\core\handlers\base.py", line 145, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "A:\Python\Enviourments\testapplication\lib\site-packages\django\core\handlers\base.py", line 143, in _get_response
    response = response.render()
  File "A:\Python\Enviourments\testapplication\lib\site-packages\django\template\response.py", line 106, in render
    self.content = self.rendered_content
  File "A:\Python\Enviourments\testapplication\lib\site-packages\rest_framework\response.py", line 70, in rendered_content
    ret = renderer.render(self.data, accepted_media_type, context)
  File "A:\Python\Enviourments\testapplication\lib\site-packages\rest_framework\renderers.py", line 725, in render
    context = self.get_context(data, accepted_media_type, renderer_context)
  File "A:\Python\Enviourments\testapplication\lib\site-packages\rest_framework\renderers.py", line 697, in get_context
    'post_form': self.get_rendered_html_form(data, view, 'POST', request),
  File "A:\Python\Enviourments\testapplication\lib\site-packages\rest_framework\renderers.py", line 512, in get_rendered_html_form
    return self.render_form_for_serializer(serializer)
  File "A:\Python\Enviourments\testapplication\lib\site-packages\rest_framework\renderers.py", line 522, in render_form_for_serializer
    {'style': {'template_pack': 'rest_framework/horizontal'}}
  File "A:\Python\Enviourments\testapplication\lib\site-packages\rest_framework\renderers.py", line 373, in render
    return template.render(context)
  File "A:\Python\Enviourments\testapplication\lib\site-packages\django\template\backends\django.py", line 61, in render
    return self.template.render(context)
  File "A:\Python\Enviourments\testapplication\lib\site-packages\django\template\base.py", line 171, in render
    return self._render(context)
  File "A:\Python\Enviourments\testapplication\lib\site-packages\django\template\base.py", line 163, in _render
    return self.nodelist.render(context)
  File "A:\Python\Enviourments\testapplication\lib\site-packages\django\template\base.py", line 937, in render
    bit = node.render_annotated(context)
  File "A:\Python\Enviourments\testapplication\lib\site-packages\django\template\base.py", line 904, in render_annotated
    return self.render(context)
  File "A:\Python\Enviourments\testapplication\lib\site-packages\django\template\defaulttags.py", line 165, in render
    values = list(values)
TypeError: 'ListSerializer' object is not iterable

这真让我发疯。它可以工作,但是看到抛出错误会很麻烦。还有其他人遇到这种情况吗?

----- Views.py -----

class KeyboardEventView(viewsets.ModelViewSet):
    queryset = KeyboardEvent.objects.all()
    serializer_class = KeyboardEventSerializer

    def get_serializer(self, *args, **kwargs):
        if "data" in kwargs:
            data = kwargs["data"]

            # check if many is required
            if isinstance(data, list):
                kwargs["many"] = True

        return super(KeyboardEventView, self).get_serializer(*args, **kwargs)

----- Models.py -----

class KeyboardEvent(models.Model):
    value = models.CharField(max_length=15)
    time = models.DateTimeField(auto_now=True)
    user_id = models.ForeignKey(UserData,on_delete=models.CASCADE)
    def __str__(self):
        return self.value

0 个答案:

没有答案