manfred-kaiser / restframework-serializer-permissions

Drop in replacement for Django Restframework Serializers to add permission based field serialization.
GNU General Public License v3.0
12 stars 1 forks source link

TypeError: __init__() got an unexpected keyword argument 'permissions' #3

Open ElijahAhianyo opened 3 years ago

ElijahAhianyo commented 3 years ago

I am getting this error when I add a permissions argument in the Serializer. Heres the stack trace:

 Exception in thread django-main-thread:
Traceback (most recent call last):
  File "C:\Users\Clearspace User\AppData\Local\Programs\Python\Python38-32\lib\threading.py", line 932, in _bootstrap_inner
    self.run()
  File "C:\Users\Clearspace User\AppData\Local\Programs\Python\Python38-32\lib\threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\Clearspace User\Documents\Programming\QCS\qcs-v2-backend\venv\lib\site-packages\django\utils\autoreload.py", line 53, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\Clearspace User\Documents\Programming\QCS\qcs-v2-backend\venv\lib\site-packages\django\core\management\commands\runserver.py", line 118, in inner_run

    self.check(display_num_errors=True)
  File "C:\Users\Clearspace User\Documents\Programming\QCS\qcs-v2-backend\venv\lib\site-packages\django\core\management\base.py", line 392, in check
    all_issues = checks.run_checks(
  File "C:\Users\Clearspace User\Documents\Programming\QCS\qcs-v2-backend\venv\lib\site-packages\django\core\checks\registry.py", line 70, in run_checks
    new_errors = check(app_configs=app_configs, databases=databases)
  File "C:\Users\Clearspace User\Documents\Programming\QCS\qcs-v2-backend\venv\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
    return check_resolver(resolver)
  File "C:\Users\Clearspace User\Documents\Programming\QCS\qcs-v2-backend\venv\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
    return check_method()
  File "C:\Users\Clearspace User\Documents\Programming\QCS\qcs-v2-backend\venv\lib\site-packages\django\urls\resolvers.py", line 408, in check
    for pattern in self.url_patterns:
  File "C:\Users\Clearspace User\Documents\Programming\QCS\qcs-v2-backend\venv\lib\site-packages\django\utils\functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\Clearspace User\Documents\Programming\QCS\qcs-v2-backend\venv\lib\site-packages\django\urls\resolvers.py", line 589, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "C:\Users\Clearspace User\Documents\Programming\QCS\qcs-v2-backend\venv\lib\site-packages\django\utils\functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\Clearspace User\Documents\Programming\QCS\qcs-v2-backend\venv\lib\site-packages\django\urls\resolvers.py", line 582, in urlconf_module
    return import_module(self.urlconf_name)
  File "C:\Users\Clearspace User\AppData\Local\Programs\Python\Python38-32\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\Users\Clearspace User\Documents\Programming\QCS\qcs-v2-backend\website\urls.py", line 34, in <module>
    path('api/v1/', include('apps.company.urls')),
  File "C:\Users\Clearspace User\Documents\Programming\QCS\qcs-v2-backend\venv\lib\site-packages\django\urls\conf.py", line 34, in include
    urlconf_module = import_module(urlconf_module)
  File "C:\Users\Clearspace User\AppData\Local\Programs\Python\Python38-32\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\Users\Clearspace User\Documents\Programming\QCS\qcs-v2-backend\apps\company\urls.py", line 2, in <module>
    from apps.company import views
  File "C:\Users\Clearspace User\Documents\Programming\QCS\qcs-v2-backend\apps\company\views.py", line 9, in <module>
    from apps.company.serializers import CompanyProfileSerializer
  File "C:\Users\Clearspace User\Documents\Programming\QCS\qcs-v2-backend\apps\company\serializers.py", line 19, in <module>
    class CompanyProfileSerializer(customSerializers.ModelSerializer):
  File "C:\Users\Clearspace User\Documents\Programming\QCS\qcs-v2-backend\apps\company\serializers.py", line 21, in CompanyProfileSerializer
    company_phone_numbers = CompanyPhoneNumberSerializer(many=True, required=False, permissions=(permissions.IsAuthenticatedOrReadOnly, ))
  File "C:\Users\Clearspace User\Documents\Programming\QCS\qcs-v2-backend\venv\lib\site-packages\rest_framework\serializers.py", line 121, in __new__
    return cls.many_init(*args, **kwargs)
  File "C:\Users\Clearspace User\Documents\Programming\QCS\qcs-v2-backend\venv\lib\site-packages\rest_framework\serializers.py", line 146, in many_init
    child_serializer = cls(*args, **kwargs)
  File "C:\Users\Clearspace User\Documents\Programming\QCS\qcs-v2-backend\venv\lib\site-packages\rest_framework\serializers.py", line 115, in __init__
    super().__init__(**kwargs)
TypeError: __init__() got an unexpected keyword argument 'permissions'

Heres my serializer serializer.py

from serializer_permissions import serializers as customSerializers
...
class CompanyProfileSerializer(customSerializers.ModelSerializer):
    company_phone_numbers = CompanyPhoneNumberSerializer(many=True, required=False, permissions=(permissions.IsAuthenticatedOrReadOnly, ))
    company_addresses = CompanyAddressSerializer(many=True, required=False, permissions=(permissions.IsAuthenticated, ))
   email_address = customSerializers.EmailField(permissions=(permissions.IsAuthenticated, ))
    class Meta:
        model = Company
        exclude = ["created_date", "created_by", "modified_date", "modified_by"]
ElijahAhianyo commented 3 years ago

Also, I could take up this issue if you'd like me to :)

manfred-kaiser commented 3 years ago

The __init__ call to serializer_permissions.serializers.ModelSerializer is missing in the stack trace.

 File "C:\Users\Clearspace User\Documents\Programming\QCS\qcs-v2-backend\apps\company\serializers.py", line 21, in CompanyProfileSerializer
   company_phone_numbers = CompanyPhoneNumberSerializer(many=True, required=False, permissions=(permissions.IsAuthenticatedOrReadOnly, ))
 File "C:\Users\Clearspace User\Documents\Programming\QCS\qcs-v2-backend\venv\lib\site-packages\rest_framework\serializers.py", line 121, in __new__
   return cls.many_init(*args, **kwargs)
 File "C:\Users\Clearspace User\Documents\Programming\QCS\qcs-v2-backend\venv\lib\site-packages\rest_framework\serializers.py", line 146, in many_init
   child_serializer = cls(*args, **kwargs)
 File "C:\Users\Clearspace User\Documents\Programming\QCS\qcs-v2-backend\venv\lib\site-packages\rest_framework\serializers.py", line 115, in __init__
   super().__init__(**kwargs)

This should be:

  File "/opt/virtualenvs/testenv/lib/python3.6/site-packages/mytestapp/serializers.py", line 42, in ResultListSerializer
    submissions = ResultSerializer(many=True, help_text="Liste der abzufragenden Submissions", permissions=("asda",))
  File "/opt/virtualenvs/testenv/lib/python3.6/site-packages/rest_framework/serializers.py", line 121, in __new__
    return cls.many_init(*args, **kwargs)
  File "/opt/virtualenvs/testenv/lib/python3.6/site-packages/serializer_permissions/serializers.py", line 65, in many_init
    child_serializer = cls(*args, **kwargs)
  File "/opt/virtualenvs/testenv/lib/python3.6/site-packages/serializer_permissions/mixins.py", line 7, in __init__
    super().__init__(*args, **kwargs)
  File "/opt/virtualenvs/testenv/lib/python3.6/site-packages/rest_framework/serializers.py", line 115, in __init__
    super().__init__(**kwargs)

Can you check, if you are using serializer_permissions.serializers.ModelSerializer as base class for CompanyPhoneNumberSerializer

Which django version and restframework version do you use?

manfred-kaiser commented 3 years ago

Also, I could take up this issue if you'd like me to :)

If this is an issue in my library, I would be glad, if you provide a pull request :smiley:

manfred-kaiser commented 3 years ago

have you solved the problem?