[Igor Sobreira] Django readonly fields in Admin

Published on: 20/02/2010 09:22h

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!

By Igor Sobreira