How to make the fields read only in django admin except for the superuser?

Viewed 3508

I have the following model defined:

class PRegistration(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True)
    create_date = models.DateTimeField(auto_now=False, auto_now_add=True)
    teamName = models.CharField(max_length=144)
    city = models.CharField(max_length=144)

How do I make all these fields read only in the admin page for all staff accounts except the superuser? I wanna do this without specifying the fields one by one so that I can reuse the code in multiple places.

2 Answers

Add the following function in the ModelAdmin subclass that you make in admin.py . The get_read_only function returns a list or a tuple which tells the fields that are to be made readonly. In the following code, the non-superuser staff accounts will see all fields as readonly, while the superuser will be able to edit all of those fields.

def get_readonly_fields(self, request, obj=None):
    if request.user.is_staff:
        if request.user.is_superuser:
            return []
        else:
            return [f.name for f in self.model._meta.fields]

Special thanks: this question and its answers

If Your model.py contains

class PRegistration(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True)
    create_date = models.DateTimeField(auto_now=False, auto_now_add=True)
    teamName = models.CharField(max_length=144)
    city = models.CharField(max_length=144)

If you want to make field user, teamName readonly for other staffs except superuser. Add this code on your admin.py of same app.

@admin.register(PRegistration)
class PRegistrationAdmin(admin.ModelAdmin):
    list_display = ['user']
    search_fields = ['user']
    readonly_fields = ['user', 'teamName']

    def get_readonly_fields(self, request, obj=None):
        if request.user.is_superuser:
            return []
        return self.readonly_fields
Related