Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: Inherit Permissions from abstract models?

Is it possible to inherit permissions from an abstract model in Django? I can not really find anything about that. For me this doesn't work!

class PublishBase(models.Model): 
    class Meta:
        abstract = True
        get_latest_by = 'created'
        permissions = (('change_foreign_items',
                        "Can change other user's items"),)

EDIT: Not working means it fails silently. Permission is not created, as it wouldn't exist on the models inheriting from this class.

like image 943
Bernhard Vallant Avatar asked Dec 04 '22 12:12

Bernhard Vallant


1 Answers

The permissions are not inherited, if the child class also defines its own class Meta. I found the following work-around, which saves from having to define the permissions again on every child model:

class AbstractBaseModel(models.Model):
    class Meta:
        abstract = True
        permissions = (("test_permission","test permission"),)


class SomeClass(AbstractBaseModel):
    name = models.CharField(max_length=255,verbose_name="Name")

    class Meta(AbstractBaseModel.Meta):
        verbose_name = ....

No need to set abstract to false in the child Meta class, since Django sets it in the parent to False when processing it! http://docs.djangoproject.com/en/dev/topics/db/models/#meta-inheritance

like image 147
Bernhard Vallant Avatar answered Dec 18 '22 12:12

Bernhard Vallant