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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:27:19+00:00 2026-05-28T06:27:19+00:00

I have a model and I’d like to restrict access to objects based on

  • 0

I have a model and I’d like to restrict access to objects based on the authentication level of the user. Anonymous users may only see a subset of the objects while authorized users have access to all objects. Reading through the django book I find that I could use checks like, is_authenticated() in my views and implement my logic based on that condition. But I don’t want these checks shotgun splattered all over my code, instead I’d like to be able to give my models some intelligence: the models should only make available the objects that are visible to the current user’s permissions.

Here are a couple of the models I’m working with:

class Collection(models.Model):
    VISIBILITY_CHOICES = (
    ('P', 'Private'),
    ('SP', 'Semi-Private'),
    ('PUB', 'Public')
    )

    name = models.CharField(max_length=40)
    visibility = models.CharField(max_length=3, choices=VISIBILITY_CHOICES)
    category = models.CharField(max_length=50)


class Image(models.Model):
    image = models.ImageField(upload_to= get_upload_to)
    collection = models.ForeignKey(Collection)

An example of a query I make would be: collection_ids = Image.objects.values_list(‘collection’,flat=True).distinct() — In this case I’d only want to check against those collection ids that the user is authorized to view (i.e public/private/semi-private).

Is it possible or do I have to put authorization logic in my views?

  • 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-28T06:27:20+00:00Added an answer on May 28, 2026 at 6:27 am

    Note that in the query example you give (Image.objects.values_list('collection',flat=True).distinct()), request is never mentioned – so how would that query be able to check the current user?

    But that doesn’t mean you have to do it in the views – you can add methods to the model itself, or even subclass QuerySet to add a method to check against the current request, like .filter(foo=bar).allowed_for_user(user).

    See this answer:

    https://stackoverflow.com/a/4576649/16361

    Which links to an ancient blog post by yours truly, although I seem to have broken the link in that answer, sorry – the correct URL is:

    EDIT: Remove a domain taken over by NSFW content and a dead link.

    Also, just to make it clear – the code itself doesn’t have to live in your views, but ultimately will need to involve the request object one way or another, so you will have to call it from your view.

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

Sidebar

Related Questions

If I have Model.objects.all() I want to get only one object for any content_object=foo,
I have model Article it has field title with some text that may contain
I have some model objects I'm using in my Java client application. Later these
I have model with ImageFile field: def upload_course_cover(object, filename): return '/media/courses/%s_%s' % (Course.objects.aggregate(Max('id'))['id__max'] +
I have model with a Foreign Key to itself like this: class Concept(models.Model): name
I have model like this: class Kaart(models.Model): name = models.CharField(max_length=200, verbose_name=Kaardi peakiri, help_text=Sisesta kaardi
I have model User. And one of controller methods is def view_messages @user =
I have model called User with a has_many relationship to UserFilter . UserFilter has
Let's say I have model like this (all irrelevant fields removed for clarity): A(Model):
I have model that looks like this: public class Lesson { public int Id

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.