djangodjango-models

Dynamic Meta attributes for Django Models?


I am trying to add a dynamic Meta attribute to all of my Django models using model inheritance, but I can't get it to work. I have a permission that I want to add to all my models like this:

class ModelA(models.Model):
    class Meta:
        permisssions =(('view_modela','Can view Model A'),)   

class ModelB(models.Model):
    class Meta:
        permisssions =(('view_modelb','Can view Model B'),)

I tried creating an abstract base class like this:

class CustomModel(models.Model):
    def __init__(self, *args, **kwargs):
        self._meta.permissions.append(('view_'+self._meta.module_name, u'Can view %s' % self._meta.verbose_name))
        super(CustomModel,self).__init__(*args, **kwargs)

class ModelA(CustomModel):
    ....

class ModelB(CustomModel):
    ...

but it's not working. Is this the right approach? Because Django uses introspection to construct the Model classes, I'm not sure if adding permissions during the __init__() of the class will even work. With my current implementation every time I access a model instance it appends another tuple of the permissions.


Solution

  • Your instinct is right that this won't work. In Django, permissions are stored in the database, which means that:

    Your goal can't be accomplished using inheritance. What you actually need here is a Python metaclass.

    This metaclass re-writes your ModelA and ModelB class definitions dynamically before they are defined, thus it doesn't require a ModelA instance, and is available to syncdb. Since Django's models also use metaclasses to build the Meta object in the first place, the only requirement is that your metaclass must inherit from the same metaclass as Django's models.

    Here's some sample code (Python 2):

    from django.db.models.base import ModelBase
    
    class CustomModelMetaClass(ModelBase):
    
        def __new__(cls, name, bases, attrs):
            klas = super(CustomModelMetaClass, cls).__new__(cls, name, bases, attrs)
            klas._meta.permissions.append(
                (
                    'view_{0.module_name}'.format(klas._meta),
                    u'Can view {0.verbose_name}'.format(klas._meta))
            )
    
            return klas
    
    class ModelA(models.Model):
    
        __metaclass__ = CustomModelMetaClass
    
        test = models.CharField(max_length=5)
    

    Python 3:

    from django.db.models.base import ModelBase
    
    class CustomModelMetaClass(ModelBase):
    
        def __new__(cls, name, bases, attrs):
            klas = super().__new__(cls, name, bases, attrs)
            klas._meta.permissions.append(
                (
                    'view_{0.module_name}'.format(klas._meta),
                    'Can view {0.verbose_name}'.format(klas._meta))
            )
    
            return klas
    
    class ModelA(models.Model, metaclass=CustomModelMetaClass):
    
        test = models.CharField(max_length=5)
    

    Note that permissions in this case will be written only on migrate. If you need to change permissions dynamically at run time base on the user, you'll want to provide your own authentication backend.