我在Django的管理員中通過TabularInline列出模型。在這個內聯中,我想使用Django的模型遍歷語法通過外鍵列出模型中引用的其他模型中的數據。例如清單Django中的相關字段ModelAdmin
class MyRelatedModel(models.Model)
name = models.CharField(max_length=50)
created = models.DateField(auto_now_add=True)
other = models.ForeignKey('MyOtherRelatedModel')
class MyOtherRelatedModel(models.Model)
name = models.CharField(max_length=50)
created = models.DateField(auto_now_add=True)
class MyRelatedModelInline(admin.TabularInline):
model = MyRelatedModel
fields = ['name', 'created', 'other__name']
#readonly_fields = ['name', 'created', 'other__name']
然而, 'other__name' 的使用拋出ImproperlyConfigured錯誤:
'MyRelatedModelInline.fields' refers to field 'other__name' that is missing from the form
是在的ModelAdmin情況下不支持的模型遍歷語法?如果支持,我在做什麼錯了?
編輯:如果我去掉readonly_fields,誤差變爲:
Caught AttributeError while rendering: 'MyMainModelAdmin' object has no attribute '__name__'
[在Django管理改變顯示ForeignKey的數據/添加頁]的可能重複(http://stackoverflow.com/questions/5665133/displaying-foreignkey-data-in-django-admin-change-添加頁面) – 2011-04-15 15:49:45