'TypeError: __init__() got an unexpected keyword argument 'many'
While trying to return a query-set using generic views, I get an error
TypeError: init() got an unexpected keyword argument 'many'
I am unable to understand the source of this error as it seems to be originating from internal libraries. I have no clue as to why this error originated as the create API works just fine.
Complete 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__'
Solution 1:[1]
Looks like it was a typo after all.
class PhotoPublicSerializer(serializers.ModelField):
class PhotoPublicSerializer(serializers.ModelSerializer):
Weird things brain does when it's tired. xD
Solution 2:[2]
I got this issue when serializer_class
was pointed to the model instead of ModelSerializer
Solution 3:[3]
The issue is caused by line:
participants = PersonPublicSerializer(many=True)
Investigating, can be seen that:
PersonPublicSerializer
is aclass
that extends fromserializers.ModelField
serializers.ModelField
is aclass
that extends fromserializers.Field
Field
, when initialized, accepts one of these args (with their defaults):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
In turn
ModelField
expects (that is, it is mandatory to have) amodel_field
argument and can additionally be called with either one ofField
's args.
Now, as you can see, neither ModelField
nor Field
has a many
arg in their declaration.
So, either PersonPublicSerializer
was not declared correctly and should implement a many
arg, or you're relying on a misunderstanding.
Source: Field
's source code
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Sandeep Balagopal |
Solution 3 | Adelin |