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 6038865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:17:05+00:00 2026-05-23T06:17:05+00:00

In the app we’re developing using Django, in some cases we need to automatically

  • 0

In the app we’re developing using Django, in some cases we need to automatically assign permissions to users for some models, that has owners (there is no rule for field’s name, it can be “user”, “owner”, “coach” etc., also there can by more than one field.) My solution is to create a decorator containing those fields names, that will be put before model definition, like this (not using django-specific code in samples):

@auto_assign_perms('owner', 'user')
class Test(Base):
    pass

Let’s assume that Base is an abstract class deriving after Django’s Model class, where I add functionality to assign permissions after object is saved. For now I only print a list of users assigned to the class. Below you can find code for the decorator and Base class:

class auto_assign_perms(object):
    def __init__(self, *users):
        self.users = users

    def __call__(self, cls):
        cls.owners.update(self.users)
        return cls


class Base(object):
    owners = set()

    def save(self, *args, **kwargs):
        for owner in self.owners:
            print owner,
        print

And my models could look like this:

@auto_assign_perms('owner', 'user')
class Test(Base):
    pass

@auto_assign_perms('coach')
class Test2(Base):
    pass

The problem is that both child classes contains all three fields ('owner', 'user', 'coach'), altough print self.__class__.__name__ in Base.save() method properly shows “Test” or “Test2”. I tried to add classmethod get_owners() in Base class and then iterating over its results, but it doesn’t helps.
How can I solve this? Maybe I should use metaclasses (I don’t get them yet)? Thanks in advance.

  • 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. Editorial Team
    Editorial Team
    2026-05-23T06:17:05+00:00Added an answer on May 23, 2026 at 6:17 am

    You need to set the list of owners, not update:

    class auto_assign_perms(object):
        def __init__(self, *users):
            self.users = users
    
        def __call__(self, cls):
            cls.owners = set(self.users) # <- here
            return cls
    
    #some tests
    @auto_assign_perms('owner', 'user')
    class Test(Base):
        pass
    
    @auto_assign_perms('coach')
    class Test2(Base):
        pass
    
    
    t = Test()
    t.save()
    t = Test2()
    t.save()
    
    >>> 
    owner user
    coach
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My app has many controls on its surface, and more are added dynamically at
Our app (already deployed) is using an Access/Jet database. The upcoming version of our
My app has a DataGridView object and a List of type MousePos. MousePos is
App A has this BroadcastReceiver in its manifest (within <application>): And this receiver: public
App has 4 view controllers; Menu, A, B and C, and a singleton class
My app is using a tab bar controller with two tab bar items: Each
My app seems to crash sometimes when I rotate the phone. ViewControllerA has a
I have a new web app that is packaged as a WAR as part
An app I'm writing always crashes on a clients computer, but I don't get
My app is installed via NSIS. I want the installer to install the program

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.