0

Given the following code:(don't mind the Fields there're just for illustration)

Models

class UserModel(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(unique=True)
Fields

class CommonInfo(models.Model):
delete = models.BooleanField(default=False)
Fields

class MyModel(CommonInfo, UserModel):
my_name = models.CharField(max_length=50, blank=False, null=False)
Fields

Serializer

class MySerializer(views.APIView):

class Meta:
    model = MyModel
    fields = '__all__'

Views

class MyViewSet(viewsets.ModelViewSet):
queryset = MyModel.objects.all()
serializer_class = MySerializer

URLs

router.register(r'studio', MyViewSet, basename='studio')

This upside model, Serializer & Views use. I use in Django REST Framework. but when I call MyModel they give me an error.

Return the following error when API call:

init() takes 1 positional argument but 2 were given

Error stack

Internal Server Error: /studio/
Traceback (most recent call last):
  File "/home/chetan/Workspace/PhotoLab/PhotoLab/venv/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/home/chetan/Workspace/PhotoLab/PhotoLab/venv/lib/python3.8/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/chetan/Workspace/PhotoLab/PhotoLab/venv/lib/python3.8/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/home/chetan/Workspace/PhotoLab/PhotoLab/venv/lib/python3.8/site-packages/rest_framework/viewsets.py", line 125, in view
    return self.dispatch(request, *args, **kwargs)
  File "/home/chetan/Workspace/PhotoLab/PhotoLab/venv/lib/python3.8/site-packages/rest_framework/views.py", line 509, in dispatch
    response = self.handle_exception(exc)
  File "/home/chetan/Workspace/PhotoLab/PhotoLab/venv/lib/python3.8/site-packages/rest_framework/views.py", line 469, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/home/chetan/Workspace/PhotoLab/PhotoLab/venv/lib/python3.8/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
    raise exc
  File "/home/chetan/Workspace/PhotoLab/PhotoLab/venv/lib/python3.8/site-packages/rest_framework/views.py", line 506, in dispatch
    response = handler(request, *args, **kwargs)
  File "/home/chetan/Workspace/PhotoLab/PhotoLab/venv/lib/python3.8/site-packages/rest_framework/mixins.py", line 45, in list
    serializer = self.get_serializer(queryset, many=True)
  File "/home/chetan/Workspace/PhotoLab/PhotoLab/venv/lib/python3.8/site-packages/rest_framework/generics.py", line 110, in get_serializer
    return serializer_class(*args, **kwargs)
TypeError: __init__() takes 1 positional argument but 2 were given
[21/Mar/2021 13:15:33] "GET /studio/ HTTP/1.1" 500 99610
5
  • can you provide the error stack trace, to see what's causing it? Commented Mar 21, 2021 at 13:08
  • @RadwanAbu-Odeh update error stack please check. Thanks in advance. happy coding. Commented Mar 21, 2021 at 13:19
  • 1
    I think the problem is that you are inheriting APIview for a serializer. Commented Mar 21, 2021 at 13:33
  • @MaratMkhitaryan can you give me suggestions on what can be changed. Commented Mar 21, 2021 at 13:37
  • Your model based serializer can inherit from serializers.ModelSerializer. Look at the DRF documentation for serializers. Commented Mar 21, 2021 at 14:17

1 Answer 1

1

You are inheriting from APIView for you MySerializer class.

to fix that, inherit from ModelSerializer from Django rest framework serializers

from rest_framework import serializers

class MySerializer(serializers.ModelSerializer):
    class Meta:
        model = MyModel
        fields = '__all__'

and everything should be resolved.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you @Radwan Abu-Odeh Happy Coding.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.