Django: Inherit from abstract models?

Is it possible to inherit permissions from an abstract model in Django? I can do nothing about it. It doesn’t work for me!

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

EDIT: Not working means that it is failing. A permission is not created because it does not exist on models inheriting from this class.

+4
source share
6 answers

Here is the link to solve the problem: http://code.djangoproject.com/ticket/10686 You need to apply the patch ... But it really works.

+2
source

Rights are not inherited if the child class also defines its own class Meta . I found the following workaround that does not allow me to determine permissions for each 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 the abstract value to false in the child meta class, because Django sets it to the parent False object when it is processed! http://docs.djangoproject.com/en/dev/topics/db/models/#meta-inheritance

+10
source

My job:

 class AbstractModelBase(models.base.ModelBase): def __new__(cls, name, bases, attrs): new = super(AbstractModelBase, cls).__new__(cls, name, bases, attrs) new._meta.permissions += (("abstract_permission", "Abstract permission"),) return new class AbstractModel(models.Model): __metaclass__ = AbstractModelBase class Meta: abstract = True 
+2
source

take a look at the following meta implementation, it adds read permissions for all django models that set the MyModelMeta class as a metaclass :

 class MyModelMeta(ModelBase): # add a read permission to each MyModelMeta model def __new__(cls, name, bases, attrs): Meta = None if "Meta" in attrs: Meta = attrs.get("Meta") if hasattr(Meta, "abstract") and getattr(Meta, "abstract"): # if the class is abstract, don't create permissions for it, just return the class object return super(MyModelMeta, cls).__new__(cls, name, bases, attrs) if not Meta: # create a new Meta Class Meta = type('Meta', (object,), {}) setattr(Meta, 'permissions',(("read_%s"%name.lower(), "Can read %s"%name.lower()),)) attrs['Meta'] = Meta return super(MyModelMeta, cls).__new__(cls, name, bases, attrs) 

create abstract django models and set the memeber for the metaclass in MyModelMeta:

 class MyAbstractModel(models.Model): __metaclass__ = MyModelMeta class Meta: abstract=True 

now create a regular django model, for example:

 class SomeModel(MyAbstractModel): someFieldName = models.CharField(max_length=256, db_index=True) 

this will create default add / change / delete_somemodel permissions, but it will also add a new read_somemodel permission.

if you also use south, use this to create additional permissions:

 from django.db.models import get_app, get_models from django.contrib.auth.management import create_permissions create_permissions(get_app(app), get_models(), 2 if settings.DEBUG else 0) 
+2
source

I am writing a test for your problem. I am using django 1.2.1 and I have a great result!

If you want to add permission to an existing model from the inheritance model, every time you change them, you need to run "syncdb". Example: 100% works (in 1.2.1 without a patch)

Now it works.

alt text http://img203.imageshack.us/img203/7500/permn.png

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) 
+1
source

In my case, explicit Meta inheritance doesn't work because of the South. See this ticket .

django-admin.py syncdb --all fixed the problem.

0
source

Source: https://habr.com/ru/post/1311716/


All Articles