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:
我尝试过一些类似的东西,包括“age()”,为内联定义一个“get_age”getter,等等。
ImproperlyConfigured: 'UserProfileInline.fieldsets[1][1]['fields']' refers to field 'age' that is missing from the form.
1 个解决方案
#1
27
Add the field to the readonly_fields
tuple as well.
也将字段添加到readonly_fields元组。
Note this only works in Django 1.2+.
注意,这只适用于Django 1.2+。
#1
27
Add the field to the readonly_fields
tuple as well.
也将字段添加到readonly_fields元组。
Note this only works in Django 1.2+.
注意,这只适用于Django 1.2+。