Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Having trouble adding rule model mixin. -- TypeError: 'class Meta' got invalid attribute(s): rules_permissions #107

Open
cdrandin opened this issue Nov 22, 2019 · 6 comments

Comments

@cdrandin
Copy link

I have tried multiple variations for support listed at https://github.com/dfunckt/django-rules#permissions-in-models

I am not sure what I am missing.

This is my current model

class Post(UUID_PK, TimeStampedModel, RulesModelBaseMixin, PolymorphicModel):
    [...]

    class Meta:
        rules_permissions = {
            "add": rules.is_staff,
            "change": rules.is_authenticated,
        }

traceback:

Traceback (most recent call last):
  File "manage.py", line 23, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/user/project/env/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command
_line
    utility.execute()
  File "/Users/user/project/env/lib/python3.7/site-packages/django/core/management/__init__.py", line 357, in execute
    django.setup()

  File "/Users/user/project/env/lib/python3.7/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/user/project/env/lib/python3.7/site-packages/django/apps/registry.py", line 114, in populate
    app_config.import_models()
  File "/Users/user/project/env/lib/python3.7/site-packages/django/apps/config.py", line 211, in import_models
    self.models_module = import_module(models_module_name)
  File "/Users/user/project/env/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/Users/user/project/app/posts/models.py", line 48, in <module>
    class Post(UUID_PK, TimeStampedModel, RulesModelBaseMixin, PolymorphicModel):
  File "/Users/user/project/env/lib/python3.7/site-packages/polymorphic/base.py", line 83, in __new__
    new_class = self.call_superclass_new_method(model_name, bases, attrs)
  File "/Users/user/project/env/lib/python3.7/site-packages/polymorphic/base.py", line 124, in call_superclass_new_method
    self, model_name, bases, attrs
  File "/Users/user/project/env/lib/python3.7/site-packages/django/db/models/base.py", line 117, in __new__
    new_class.add_to_class('_meta', Options(meta, app_label))
  File "/Users/user/project/env/lib/python3.7/site-packages/django/db/models/base.py", line 321, in add_to_class
    value.contribute_to_class(cls, name)
  File "/Users/user/project/env/lib/python3.7/site-packages/django/db/models/options.py", line 196, in contribute_to_class
    raise TypeError("'class Meta' got invalid attribute(s): %s" % ','.join(meta_attrs))
TypeError: 'class Meta' got invalid attribute(s): rules_permissions

I also tried checking django polymorphism to see if it was an issue there.

https://github.com/django-polymorphic/django-polymorphic/blob/master/polymorphic/models.py

@cdrandin cdrandin changed the title Having trouble adding rule model mixin. Having trouble adding rule model mixin. -- TypeError: 'class Meta' got invalid attribute(s): rules_permissions Nov 22, 2019
@dfunckt
Copy link
Owner

dfunckt commented Nov 24, 2019

You need to specify the custom metaclass, see that part of the README again.

@cdrandin
Copy link
Author

@dfunckt yes I have tried that. I guess I do not understand how to set it up exactly.

I have tried multiple variations of inheriting at the class model level and meta level.

@tadeo
Copy link

tadeo commented Mar 28, 2020

Not sure about this but after reading the docs I suspect you might:

class Post(RulesModelMixin, PolymorphicModelBase, UUID_PK, TimeStampedModel):
    class Meta(RulesModelBase):

@krystofbe
Copy link

did anyone get this working? i stumbled over this problem too and can't get it working. Adding RulesModelBaseMixin to the Meta class did not solve it

@tadeo
Copy link

tadeo commented Jul 23, 2020

Did you try to adding RulesModelBase as 'metaclass' parameter as suggested in the documentation?
class MyModel(RulesModelMixin, YourBaseModel1, YourBaseModel2, Model, metaclass=RulesModelBase)
Another option I think could work is to inherit from PolymorphicModel instead of PolymorphicModelBase, and/or consider using 'polymorphic.utils.get_base_polymorphic_model(ChildModel, allow_abstract=False)' or 'polymorphic.compat.with_metaclass(meta, *bases)'.
Also, I've edited my previous comment to add another option I would try.
I hope I can bring something helpful to the table.

@SidWeng
Copy link

SidWeng commented Dec 10, 2021

I solve it by:

class RulesPolyModelBase(RulesModelBaseMixin, PolymorphicModelBase):
    pass


class MyModel(RulesModelMixin, PolymorphicModel, metaclass=RulesPolyModelBase):

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants