2011-10-07 130 views
1

我有以下的模型和TabularInline子類:Django管理TabularInline抱怨缺少現場

class SomeModel(models.Model): 
    name = models.CharField(max_length=50) 

class SomeModelInline(admin.TabularInline): 
    model = SomeModel 

class SomeOtherModelAdmin(admin.ModelAdmin): 
    inlines = [SomeModelInline] 

沒有明確指定TabularInline的田地,Django的管理顯示的字段「ID」和「名稱」。然而,當我嘗試這樣做:

class SomeModelInline(admin.TabularInline): 
    model = SomeModel 
    fields ['id','name'] 

的Django拋出ImproperlyConfigured異常:

'SomeModelInline.fields' refers to field 'id' that is missing from the form. 

這是怎麼回事?爲什麼我不能明確指定id,即使Django顯然能夠訪問它?

回答

2

ID是不可編輯,默認情況下在線顯示可編輯的字段,但你可以展現不可編輯的字段,以及

django docs

字段可以在ModelAdmin.readonly_fields包含定義的值爲 顯示爲只讀。

所以先加「身份證」到readonly_fields,然後將其添加到域

+0

這不是真的是我問,但它確實讓我來解決此問題。 – Cerin

相關問題