这是这个question的后续.如何在父模型的内联中显示子模型上定义的属性?为了说明,我有这个模型:
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
问题是:我如何在用户的内联中显示"年龄"?这就是我所拥有的:
class UserProfileInline(admin.StackedInline):
model = UserProfile
fk_name = 'user'
max_num = 1
fieldsets = [
('Demographics', {'fields': ['birthday', 'age']}),
]
我try 了一些类似的操作,包括‘age()’、为内联定义一个‘get_age’getter等.它们会导致此错误的某些版本:
ImproperlyConfigured: 'UserProfileInline.fieldsets[1][1]['fields']' refers to field 'age' that is missing from the form.