Igor Sobreira

Django readonly fields in Admin

Finally Django has implemented readonly fields in Admin interface. It's a new feature in 1.2 which is in alpha version at this moment, but will be released soon.

The feature is really simple and is already documented so I won't explain how it works. But there is one implementation detail not documented that gives much more power to this feature. If you check the admin sources, you'll find a method called get_readonly_fields() in django/contrib/admin/options.py, by default it just returns self.readonly_fields, which is the normal usage, but since you receive the request object as a parameter you can enable or not some fields depending on the logged in user.

Here is an example where a slug field can just be edited by superusers:

class FooModelAdmin(admin.ModelAdmin):
    ...
    def get_readonly_fields(self, request, obj=None):
        if request.user.is_superuser:
            return ()
        return ('slug',)

There are lot's of interesting new features in Django 1.2, I'm looking forward for it!

blog comments powered by Disqus

My name is Igor Sobreira and is my website. I'm a software developer working at Real Geeks. I currently live in Hawaii doing the two things that I like most: windsurfing and writing software.

See more about me and get in touch.