Django - 动态设置list_display的内容(管理视图)
有没有办法在每次过滤模型时重置某个模型的 list_display
(在管理视图中)的内容? (这样按不同对象进行过滤将在显示表中提供不同数量的列)。
例如:
我的 django 应用程序中有三个模型:Child
、Family
、Pet
。 Child
有一个 Family
的外键,Pet
也有一个 Family
的外键。
现在,在 Child
的管理视图中,我希望能够按 Family
进行过滤,过滤后,表格将显示孩子的姓名以及每个人的姓名他的家人拥有的宠物之一。这样,显示的表格中的列数会根据进行过滤的 Family
对象的宠物数量而变化。
谢谢。
Is there a way to reset the contents of the list_display
(in the admin view) for some model each time the model is filtered? (so that filtering by different objects will give a diffrent number of columns in the display table).
For example:
I have three model in my django application: Child
, Family
, Pet
. Child
has a foregin key to Family
, and Pet
also has a foregin key to Family
.
Now, in the admin view of Child
, I want to be able to filter by Family
, and after the filtering, the table will display the child's name and the name of each one of the pets that his family owns. That way the number of columns in the displayed table changes according to the number of pets of the Family
object the filtering was done by.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
list_display
在admin.py中设置,需要重启服务器才能生效。因此,它无法即时修改。此外,Django 似乎不支持以这种方式显示 ManyToMany 字段,因此您无法“开箱即用”地在列表视图中显示宠物名称。根据 文档 :
list_display
is set in admin.py, and requires a server restart to become effective. As a result, it cannot be modified on the fly.Additionally, Django does not seem to support the display of ManyToMany fields in this manner, so you cannot display the pet names in the list view "out of the box." According to the documentation: