【发布时间】:2020-08-13 14:46:54
【问题描述】:
型号: 我有一个这样的模型
class TestModel1(models.Model):
bookingid = models.ForeignKey(paymenttable) // foreign key
name = models.CharField(max_length=100, null=False, db_index=True)
// I want this to be displayed in both the list view and detail view
@property
def custom_field(self):
return self.bookingid.username
Admin.py
class MyAdmin(ReadOnlyAdminFields,admin.ModelAdmin):
// this works and i get custom_field in list view
list_display = ('custom_field', 'name')
readonly = ('custom_field', 'name')
// this dosent work and gives error
fields = ('custom_field', 'name')
错误:未知字段 custom_field。检查 MyAdmin 类的字段/字段集/排除属性
【问题讨论】:
-
对于
fields试试fields = ('bookingid__username', 'name') -
@wfehr ,是的,试过这个,得到了同样的错误 - 未知字段 bookingid__username。检查 MyAdmin 类的字段/字段集/排除属性
标签: python django python-2.7 django-models django-admin