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

The Archive Base Latest Questions

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

I created some code to differentiate between two usergroups in Django admin, resulting in

  • 0

I created some code to differentiate between two usergroups in Django admin, resulting in showing all fields readonly or only some of them, which are set directly in the ModelAdmin class.

At first here is the code:

class PersonAdmin(admin.ModelAdmin):
    readonly_fields = ('created_at','created_by',)

def get_form(self, request, obj=None, **kwargs):
    if obj:     # we are in edit mode
        if request.user.is_superuser:
            self.readonly_fields = ()
        else:
            for group in request.user.groups.all():
                if str(group) == 'readonlyuser':
                    allfields = tuple(obj._meta.get_all_field_names())
                    self.readonly_fields = allfields

    return super(PersonAdmin, self).get_form(request, obj, **kwargs)

I divide between the groups and set the fields accordingly. Everything works fine if users from the two groups are not logged in at the same time! After a ‘readonly’ user logged in, the adminuser will get all fields readonly too.

My inspections provided a solution also:
If I put an additional if statement for the adminuser within the for block everything works as expected.

if str(group) == 'adminuser':
    self.readonly_fields = PersonAdmin.readonly_fields

Why is that and what’s happening there?

I have no special cache settings made and it happens on the dev server as well as on an Apache with WSGI.

From my understanding request.user.groups.all() should return all groups the currently logged in user belongs to. Where does Django get the allfields (readonly) from, if another user on a different IP and session match this if block?

  • 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-28T15:25:28+00:00Added an answer on May 28, 2026 at 3:25 pm

    The ModelAdmin is only instantiated once for all requests that it receives. So when you define the readonly fields like that, you’re setting it across the board permanently.

    As long as you’re running Django 1.2+, there’s a get_readonly_fields method you can use instead for exactly this purpose:

    class MyModelAdmin(admin.ModelAdmin):
        ...
    
        def get_readonly_fields(self, request, obj=None):
            if request.user.is_superuser:
                return super(MyModelAdmin, self).get_readonly_fields(request, obj)
            else:
                return ('created_at', 'created_by')
    

    Remove the readonly_fields attribute from your ModelAdmin or set it to the fields that should be readonly for everyone. Then, in the else block specify all the fields that should be readonly only for non-superusers.

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

Sidebar

Related Questions

I have created some python code which creates an object in a loop, and
Thanks to this site and a few others, I've created some simple code to
I remember few weeks ago when I reorgnized our code and created some namespaces
I'm creating some custom components and backing code. I've created a Flex library project
I have created some code to popup a confirm dialog if a user tries
I'm new to Django. Today I created some Django custom tags which is not
I've created some code that automatically posts one message a day on a facebook
I've created some code that will dynamically generate a bunch of different LinkButtons. I've
So I have created some demo code, see below. What I am seeing is
OK so my issue is that I created some code in php to connect

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.