在Django管理员中编辑/显示主键 [英] Edit/show Primary Key in Django Admin

查看:243
本文介绍了在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...

这篇关于在Django管理员中编辑/显示主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆