dfunckt / django-rules

Awesome Django authorization, without the database

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

cdrandin opened this issue · comments

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

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

@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.

commented

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

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

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

commented

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.

I solve it by:

class RulesPolyModelBase(RulesModelBaseMixin, PolymorphicModelBase):
    pass


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