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

  • SEARCH
  • Home
  • 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 8277433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T08:41:29+00:00 2026-06-08T08:41:29+00:00

I am trying to restrict users in groups from being able to change fields

  • 0

I am trying to restrict users in groups from being able to change fields their group does not have permission to change. For example:

class StudentReferral(models.Model):
    teacher = models.CharField(max_length=50)
    referral_first_name = models.CharField(max_length=50)
    referral_last_name = models.CharField(max_length=50)
    accepted = models.BooleanField(blank=True)

Now the teachers are all in one user group and the person who accepts or declines the referral is in another user group. The teacher user group should only be able to edit the following fields: teacher, referral_first_name and referral_last_name. The other user group should only be able to edit the accepted field. Both groups should be able to see all of the fields.

Is there something built into django that makes this possible or a custom way of doing this?

  • 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-06-08T08:41:30+00:00Added an answer on June 8, 2026 at 8:41 am

    Override ModelAdmin.get_fieldsets, and do something like the following:

    class MyModelAdmin(admin.ModelAdmin):
        ...
        fieldsets = (
            ... # Standard/all fields
        )
        teacher_fieldsets = (
            ... # Only fields teachers can see
        )
    
        def get_fieldsets(self, request, obj=None):
            if request.user.groups.filter(name='Teachers').exists():
                return self.teacher_fieldsets
            else:
                return super(MyModelAdmin, self).get_fieldsets(request, obj=obj)
    

    EDIT

    Sorry, I missed the bit about they should still be able to see all fields, just not edit them. I’ve left the original answer intact, as it might still be of use to you. For this, though, you’ll need to override ModelAdmin.get_readonly_fields:

    class MyModelAdmin(admin.ModelAdmin):
        ...
        readonly_fields = ('teacher', 'referral_first_name', 'referral_last_name')
        teacher_readonly_fields = ('accepted',)
    
        def get_readonly_fields(self, request, obj=None):
            if request.user.groups.filter(name='Teachers').exists():
                return self.teacher_readonly_fields
            else:
                return super(MyModelAdmin, self).get_readonly_fields(request, obj=obj)
    

    I’m assuming here that the choices are only teacher or “other user”. If there’s other types to consider, or you don’t want the fields limited at all in one circumstance, you might want to remove the readonly_fields attribute and replace it with something like other_readonly_fields, and branch accordingly (the default value for readonly_fields is only those fields that have editable=False on the model).

    Also, be aware that if a field is required, you can’t make it read-only as well. If some of these are required fields, you’ll need to override ModelForm.__init__ as well, to make them not required in the circumstances where they will be read-only, which requires some extra hackery (ModelForm doesn’t have access to request, normally):

    class MyModelForm(forms.ModelForm):
        class Meta:
            model = MyModel
    
        def __init__(self, *args, **kwargs):
            self.request = kwargs.pop('request')
            super(MyModelForm, self).__init__(*args, **kwargs)
    
            if self.request is not None:
                if self.request.user.groups.filter(name='Teachers').exists():
                    self.fields['accepted'].required = False
    
    class MyModelAdmin(admin.ModelAdmin):
        form = MyModelForm
        ...
        def get_form(self, request, obj=None, **kwargs):
            ModelForm = super(MyModelAdmin, self).get_form(request, obj=obj, **kwargs)
            class WrapperModelForm(ModelForm):
                def __new__(cls, *args, **kwargs):
                    kwargs['request'] = request
                    return ModelForm(*args, **kwargs)
            return WrapperModelForm
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to restrict content on a canvas app to users who have liked
I'm trying to restrict TeamCity users to members of a specific AD group (FNC_TEAMCITY_USERS).
Trying to build out an exception if move.UserId does not equal currentUserId then Redirect
Using geolocation data of websocket users on a specific server, I'm trying to restrict
I have an Image uploading application running on carrierwave which restrict user from uploading
I have built a ruby on rails app that lets users track their workouts.
I have an app which I'm trying to restrict permissions to while I'm doing
I have a few buttons in my webpage and was trying to restrict the
I am trying to restrict a user from signing more than once (forcing the
I'm trying to restrict access to Projects that a user did not create. This

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.