Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 169161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T12:34:52+00:00 2026-05-11T12:34:52+00:00

I am trying to add a dynamic Meta attribute to all of my Django

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T12:34:53+00:00Added an answer on May 11, 2026 at 12:34 pm

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

    • they need to be available at the class level when syncdb is run in order to populate the auth_permission table (and your approach requires an instance, which won’t be made during syncdb)
    • even if you did add it to _meta.permissions in __init__, the User object wouldn’t pick it up in any permission check calls because those consult the permissions table in the DB (and a cache of that table, at 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 73k
  • Answers 73k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Does your review model have the corresponding belongs_to :user belongs_to… May 11, 2026 at 1:59 pm
  • added an answer Without further information regarding the structure of the tables, I… May 11, 2026 at 1:59 pm
  • added an answer try using query = query.OrderBy(c => c.FirstName); or var sortedQuery… May 11, 2026 at 1:59 pm

Related Questions

I am trying to add a project reference or swc to papervision in FlashDevelop
I am trying to add a timed delay in a C++ program, and was
I am trying to add a draggable object to to a simple html page.
I am trying to add a UIButton at runtime however it is not visible.
I am trying to add a link into the pop-up text bubble of a
I am trying to display a 'Software Release' table in an asp.net dynamic data
I'm wondering if there's a better way to add dynamic methods to an existing
I am trying to leverage ORM given the following requirements: 1) Using .NET Framework
How would one display any add content from a dynamic aspx page? Currently I

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.