views:

100

answers:

3

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.

A: 

Here is link for resolve you issue: http://code.djangoproject.com/ticket/10686 Need to apply patch... But it realy works.

Saff
As I get it permissions should also be inherited without this patch?(if no explicit permissions defined in children, and the %(class)s problem)
lazerscience
+1  A: 

I write test for your issue. I use django 1.2.1 and i have great result!

If you want to add permission to your existing model from inherit model, every time when you change them you need to run "syncdb". Example 100% works.(in 1.2.1 without patch)

It now works.

alt text

Example:

from django.db import models
from django.contrib import admin

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


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

admin.site.register(SomeClass)
Saff
thanks, good to know that it is supposed to work like this... so i have to find out why it doesnt!
lazerscience
Found out what the problem is: If the child class has her own inner Meta class (that doesnt define any permissions), the permissions are not inherited!
lazerscience
+1  A: 

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

lazerscience