在Django管理员中编辑/显示主键

问题描述:

看来,Django会隐藏在Django管理界面中显示/编辑的标记主键的字段。

It appears Django hides fields that are flagged Primary Key from being displayed/edited in the Django admin interface.

我想输入数据,其中我可能也可能不想指定主键。我如何在管理界面中显示主键,如何指定它可选?

Let's say I'd like to input data in which I may or may not want to specify a primary key. How would I go about displaying primary keys in the admin interface, and how could I make specifying it optional?

我也想在Django管理员中只显示id(主键),但不一定要编辑它。我刚把它添加到 readonly_fields 列表中,它显示正常。 IE:

I also wanted to simply show the 'id' (primary key) within the Django admin, but not necessarily edit it. I just added it to the readonly_fields list, and it showed up fine. IE:

class StudentEnrollmentInline(admin.TabularInline):
    model = Enrollment
    readonly_fields=('id',)

而如果我尝试将其添加到fields列表中,Django对我说这个领域不存在...

whereas if I tried to add it to the 'fields' list, Django got upset with me, saying that field didn't exist...