views:

82

answers:

1

This is a follow-up to this question. How do I display properties defined on a child model in an inline on the parent? To illustrate, I have this model:

class UserProfile(models.Model):
    user = models.ForeignKey(User, unique=True, primary_key=True, related_name='profile')
    ...
    @property
    def age(self):
        if self.birthday is None:
            return None

        td = datetime.date.today() - self.birthday
        return td.days / 365

Question is: how do I show 'age' in an inline on User? This is what I have:

class UserProfileInline(admin.StackedInline):
    model = UserProfile
    fk_name = 'user'
    max_num = 1
    fieldsets = [
        ('Demographics', {'fields': ['birthday', 'age']}),
    ]

I've tried a few things like this, including 'age()', defining a 'get_age' getter for the Inline, etc. They result in some version of this error:

ImproperlyConfigured: 'UserProfileInline.fieldsets[1][1]['fields']' refers to field 'age' that is missing from the form.
+1  A: 

Add the field to the readonly_fields tuple as well.

Note this only works in Django 1.2.

Daniel Roseman
When I add readonly_fields = ['age',] to class UserProfileInline as shown above, I don't get an error but it doesn't show anything in admin. Am I missing something? I'm on Django 1.2, but still learning my way around - thanks for your help.
Joe
That's strange - it should work. Are you sure the server has restarted?
Daniel Roseman