尝试使用通用视图返回查询集时,出现错误
TypeError: init ()获得了意外的关键字参数“很多”
我无法理解此错误的来源,因为它似乎源自内部库。我不知道为什么这个错误源于create API的正常工作。
完成StackTrace
System check identified 1 issue (0 silenced).
July 16, 2018 - 13:25:43
Django version 1.11.7, using settings 'BirthdayPostStash.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
<QuerySet [<Photos: 1>, <Photos: 2>, <Photos: 3>, <Photos: 4>, <Photos: 5>, <Photos: 6>, <Photos: 7>, <Photos: 8>, <Photos: 9>, <Photos: 10>, <Photos: 11>, <Photos: 12>]>
Internal Server Error: /photos/list-photo/
Traceback (most recent call last):
File "/home/t2b/env/bpstash/local/lib/python2.7/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/home/t2b/env/bpstash/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/t2b/env/bpstash/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/t2b/env/bpstash/local/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
return view_func(*args, **kwargs)
File "/home/t2b/env/bpstash/local/lib/python2.7/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/home/t2b/env/bpstash/local/lib/python2.7/site-packages/rest_framework/views.py", line 489, in dispatch
response = self.handle_exception(exc)
File "/home/t2b/env/bpstash/local/lib/python2.7/site-packages/rest_framework/views.py", line 449, in handle_exception
self.raise_uncaught_exception(exc)
File "/home/t2b/env/bpstash/local/lib/python2.7/site-packages/rest_framework/views.py", line 486, in dispatch
response = handler(request, *args, **kwargs)
File "/home/t2b/env/bpstash/local/lib/python2.7/site-packages/rest_framework/generics.py", line 201, in get
return self.list(request, *args, **kwargs)
File "/home/t2b/env/bpstash/local/lib/python2.7/site-packages/rest_framework/mixins.py", line 44, in list
serializer = self.get_serializer(page, many=True)
File "/home/t2b/env/bpstash/local/lib/python2.7/site-packages/rest_framework/generics.py", line 112, in get_serializer
return serializer_class(*args, **kwargs)
File "/home/t2b/env/bpstash/local/lib/python2.7/site-packages/rest_framework/fields.py", line 1774, in __init__
super(ModelField, self).__init__(**kwargs)
TypeError: __init__() got an unexpected keyword argument 'many'
[16/Jul/2018 13:25:45] "GET /photos/list-photo/ HTTP/1.1" 500 111194
models.py
class Photos(models.Model):
owner = models.ForeignKey(User, related_name="photos_owner")
photo = models.ImageField(upload_to=get_image_path, blank=True)
participants = models.ManyToManyField(
Person,
related_name="photos_participants",
null=True,
blank=True)
views.py
class ListPhoto(generics.ListAPIView):
permission_classes = (permissions.IsAuthenticated,)
serializer_class = PhotoPublicSerializer
model = Photos
def get_queryset(self):
return self.model.objects.all()
serializer.py
class PhotoPublicSerializer(serializers.ModelField):
participants = PersonPublicSerializer(read_only=True)
class Meta:
model = Photos
fields = '__all__'
personserializer.py
class PersonPublicSerializer(serializers.ModelSerializer):
class Meta:
model = Person
fields = '__all__'
答案 0 :(得分:5)
看起来毕竟是错字。
class PhotoPublicSerializer(serializers.ModelField):
class PhotoPublicSerializer(serializers.ModelSerializer):
大脑在疲倦时会做些奇怪的事情。 xD
答案 1 :(得分:1)
当serializer_class
指向模型而不是ModelSerializer
时我遇到了这个问题
答案 2 :(得分:0)
问题是由以下行引起的:
participants = PersonPublicSerializer(many=True)
调查中可以看出:
PersonPublicSerializer
是从class
serializers.ModelField
serializers.ModelField
是从class
serializers.Field
Field
在初始化时接受以下args之一(及其默认值):
read_only=False
write_only=False
required=None
default=empty
initial=empty
source=None
label=None
help_text=None
style=None
error_messages=None
validators=None
allow_null=False
依次ModelField
期望(也就是说,必须具有一个model_field
自变量,并且可以另外使用{{1 }}的参数。
现在,如您所见,Field
和ModelField
的声明中都没有Field
arg。
因此,many
的声明不正确,应该实现一个PersonPublicSerializer
arg,或者您是在误解。