Joe*_*Joe 24 django django-models django-admin
这是这个问题的后续行动.如何在父级的内联中显示在子模型上定义的属性?为了说明,我有这个模型:
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
Run Code Online (Sandbox Code Playgroud)
问题是:如何在用户的内联中显示"年龄"?这就是我所拥有的:
class UserProfileInline(admin.StackedInline):
model = UserProfile
fk_name = 'user'
max_num = 1
fieldsets = [
('Demographics', {'fields': ['birthday', 'age']}),
]
Run Code Online (Sandbox Code Playgroud)
我尝试过这样的一些事情,包括'age()',为Inline定义'get_age'getter等等.它们导致了这个错误的某些版本:
ImproperlyConfigured: 'UserProfileInline.fieldsets[1][1]['fields']' refers to field 'age' that is missing from the form.
Run Code Online (Sandbox Code Playgroud)
Dan*_*man 37
也将字段添加到readonly_fields
元组.
请注意,这仅适用于Django 1.2+.
归档时间: |
|
查看次数: |
12505 次 |
最近记录: |