在 Django Admin 中编辑/显示主键 [英] Edit/show Primary Key in Django Admin
问题描述
似乎 Django 隐藏了标记为 Primary Key 的字段,使其无法在 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
列表中,它显示得很好.即:
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',)
而如果我试图将它添加到字段"列表中,Django 对我很不高兴,说该字段不存在......
whereas if I tried to add it to the 'fields' list, Django got upset with me, saying that field didn't exist...
这篇关于在 Django Admin 中编辑/显示主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!