具有ModelSerializer和Post方法的Django

时间:2018-10-17 16:30:56

标签: django django-rest-framework

我对Django来说还很陌生,现在我正在为后端服务器实现REST API,并且在设置POST方法时遇到了麻烦。当请求的json字符串进行序列化时,一切都会停止。这是我的代码段

class Sighting (models.Model):
    userID = models.IntegerField()
    registeredID = models.IntegerField()
    cardID = models.IntegerField()

class SightingSerializer (serializers.ModelSerializer):
    class Meta:
        model = Sighting
        fields = "__all__"

class SightingsList (QGAPIView):
    def get (self, request, userID, format=None):
        self.check_user(userID)
        data = Sighting.objects.filter (userID = userID)
        if not data:
            return Response (None)
        else:
            serializer = SightingSerializer (data, many=True)
            return Response (serializer.data)

    def post (self, request, userID, format=None):
        self.check_user(userID)
        self.check_token (request, userID)
        obj = Sighting.objects.filter (userID = userID)
        data = self.parse_data (request)
        print (data)
        return self.get_serialized_responds (SightingSerializer, 
            obj, data[0], True)

这是get_serialized_responds方法:

def get_serialized_responds (self, serializerClass, obj, data, many=False):
        print ("Serializing")
        serializer = serializerClass (obj, data, many=many)
        print (data)
        if not serializer.is_valid():                
            print ("I am here")
            return Response ('Wrong data type', status=406)
        else:
            serializer.save()
            return Response (serializer.data)

我正在传递以下字符串

'{"Items":[{"userID":0,"registeredID":0,"cardID":0}, {"userID":0,"registeredID":1,"cardID":0}]}'

后来被剥离为:

[{'registeredID': 0, 'userID': 0, 'cardID': 0}, {'registeredID': 1, 'userID': 0, 'cardID': 0}]

最后,我得到了此追溯:

Internal Server Error: /users/0/sightings/
Traceback (most recent call last):
  File "/root/env/lib/python3.5/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/root/env/lib/python3.5/site-packages/django/core/handlers/base.py", line 126, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/root/env/lib/python3.5/site-packages/django/core/handlers/base.py", line 124, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/root/env/lib/python3.5/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/root/env/lib/python3.5/site-packages/django/views/generic/base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "/root/env/lib/python3.5/site-packages/rest_framework/views.py", line 483, in dispatch
    response = self.handle_exception(exc)
  File "/root/env/lib/python3.5/site-packages/rest_framework/views.py", line 443, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/root/env/lib/python3.5/site-packages/rest_framework/views.py", line 480, in dispatch
    response = handler(request, *args, **kwargs)
  File "/root/earthx/api/apiviews.py", line 225, in post
    obj, data[0], True)
  File "/root/earthx/api/apiviews.py", line 95, in get_serialized_responds
    serializer = serializerClass (obj, data, many=many)
  File "/root/env/lib/python3.5/site-packages/rest_framework/serializers.py", line 124, in __new__
    return cls.many_init(*args, **kwargs)
  File "/root/env/lib/python3.5/site-packages/rest_framework/serializers.py", line 145, in many_init
    child_serializer = cls(*args, **kwargs)
  File "/root/env/lib/python3.5/site-packages/rest_framework/serializers.py", line 589, in __init__
    assert self.child is not None, '`child` is a required argument.'
AssertionError: `child` is a required argument.

所以,我的目标是发送一个对象列表,每个对象对应一个模型。有人可以解释一下至少在哪里看吗?谢谢!

0 个答案:

没有答案