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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T14:30:04+00:00 2026-05-12T14:30:04+00:00

I’m trying to implement row-based security checks for Django models. The idea is that

  • 0

I’m trying to implement row-based security checks for Django models. The idea is that when I access model manager I specify some additional info which is used in database queries so that only allowed instances are fetched from database.

For example, we can have two models: Users and, say, Items. Each Item belongs to some User and User may be connected to many Items. And let there be some restrictions, according to which a user may see or may not see Items of another User. I want to separate this restrictions from other query elements and write something like:

items = Item.scoped.forceRule('user1').all()  # all items visible for 'user1'

or

# show all items of 'user2' visible by 'user1'
items = Item.scoped.forceRule('user1').filter(author__username__exact = 'user2') 

To acheive this I made following:

class SecurityManager(models.Manager):

    def forceRule(self, onBehalf) :
        modelSecurityScope = getattr(self.model, 'securityScope', None)
        if modelSecurityScope :
            return super(SecurityManager, self).get_query_set().filter(self.model.securityScope(onBehalf))
        else :
            return super(SecurityManager, self).get_query_set()

    def get_query_set(self) :
        #
        # I need to know that 'onBehalf' parameter here
        #  
        return super(SecurityManager, self).get_query_set()

class User(models.Model) :
    username = models.CharField(max_length=32, unique=True)

class Item(models.Model) :
    author = models.ForeignKey(User)
    private = models.BooleanField()
    name = models.CharField(max_length=32)

    scoped = SecurityManager()

    @staticmethod
    def securityScope(onBehalf) :
        return Q(author__username__exact = onBehalf) | Q(bookmark__private__exact = False)

For shown examples it works fine, but dies on following:

items = Item.scoped.forceRule('user1').filter(author__username__exact = 'user2') # (*)
items2 = items[0].author.item_set.all()  # (**)

Certainly, items2 is populated by all items of ‘user2’, not only those which conform the rule. That is because when all() is executed SecurityManager.get_query_set() has no information about the restriction set. Though it could. For example, in forceRule() I could add a field for every instance and then, if I could access that field from manager, apply the rule needed.

So, the question is – is there any way to pass an argument provided to forceRule() in statement (*) to manager, called in statement (**).

Or another question – am I doing strange things that I shouldn’t do at all?

Thank you.

  • 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-12T14:30:05+00:00Added an answer on May 12, 2026 at 2:30 pm

    From my reading of the documentation I think there are two problems:

    1. The SecurityManager will not be used for the related objects (and instance of django.db.models.Manager will be used instead)
    2. You can fix the above, but the documentation goes to great lengths to specify that get_query_set() should not filter out any rows for related queries.

    I suggest creating a function that takes a QuerySet and applies the filter you need to it. This can then be used whenever you get to a QS of Items and want to process them further.

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If I understand correctly, the question is “why (when) do… May 12, 2026 at 11:19 pm
  • Editorial Team
    Editorial Team added an answer Usually, when you need both protected and unprotected UAC modes… May 12, 2026 at 11:19 pm
  • Editorial Team
    Editorial Team added an answer Default buttons are affected by Windows Themes. You'll notice your… May 12, 2026 at 11:19 pm

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want use html5's new tag to play a wav file (currently only supported
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
In order to apply a triggered animation to all ToolTip s in my app,
I have a French site that I want to parse, but am running into

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.