views:

37

answers:

2

Using Django 1.1:

The Django admin docs describe using arbitrary methods or attributes on a ModelAdmin object in the list_display class attribute. This is a great mechanism for displaying arbitrary information in the list display for a Model. However, there does not appear to be a similar mechanism for the change form page itself. What is the simplest way to accomplish this useful little feature to display arbitrary, non-field-derived information on the ModelAdmin change form page?

A concrete example of the desired setup:

class CustomUserAdmin(UserAdmin):
    def registration_key(self, obj):
        """Special method for looking up and returning the user's registration key
        """
        return 'the_key'

    list_display = ('email', 'first_name', 'last_name', 'is_active', 'is_staff', 
                    'registration_key')  # <- this works

    fields = ('email', 'first_name', 'last_name', 'is_active', 'is_staff',
              'registration_key')  # <- this DOESN'T work?
+1  A: 

I've done this before by overriding the template for the change form, and accessing custom methods on the model. Using fields is asking the admin to try to add a form field for your method.

Bob
Admin templates fill me with fear and trembling, but I might have to consider this.
David Eyk
A: 

Add the method to the 'readonly_fields' tuple as well.

Daniel Roseman
I wish! `readonly_fields` is new in Django 1.2, and I'm stuck with 1.1 for now.
David Eyk
You're out of luck then, I'm afraid - there's no way to do this in 1.1 without severe hacking.
Daniel Roseman