Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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__'
like image 376
rahul1205 Avatar asked Jul 16 '18 13:07

rahul1205


1 Answers

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

like image 145
rahul1205 Avatar answered Sep 17 '22 15:09

rahul1205