take a look at the following meta implementation,
it adds read permissions to all django models that set MyModelMeta Class to be thier 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 an abstract django models and set the meta class memeber to MyModelMeta:
class MyAbstractModel(models.Model):
__metaclass__ = MyModelMeta
class Meta:
abstract=True
now, create a normal django model like so:
class SomeModel(MyAbstractModel):
someFieldName = models.CharField(max_length=256, db_index=True)
this will generate the default add/change/delete_somemodel permissions, but also
it will add a new read_somemodel permission.
if you're also using south, use this to generate the extra 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)