Django Admin:需要有条件地显示字段 [英] Django Admin: Need to conditionally display fields
问题描述
根据其他字段的值,有条件地显示管理域的最佳方法是什么?
What is the best way to conditionally display a field in the admin depending upon the values of other fields?
特别是我在考虑add_form和change_form 。每当选择某个选择时,我想隐藏或禁用某些字段。
In particular I'm thinking about the add_form and change_form. Whenever a certain choice is selected I'd like to hide or disable some fields.
我认为这可能需要一个javascript解决方案,但是想知道是否有一个更好的(即内置的)方法来做到这一点。
I'm thinking that this might require a javascript solution, but am wondering if there is a better (i.e. builtin) way to do this.
推荐答案
伯恩哈德是对的。您可能可以劫持管理员视图和模板以有条件地显示/不显示字段的小部件,但是如果要根据管理员中的用户行为动态执行,则将使用javascript。
Bernhard is right. You might be able to hack the admin view and template to conditionally show/not show widgets for a field, but if you want to do it dynamically based on user behaviors in the admin, you'll be using javascript.
这不是太可怕了。至少Django管理员模板具有模型和实例特定的ID,可以精确控制您的显示/隐藏行为。
It's not so terrible, though. At least the Django admin templates have model- and instance-specific ids to give you granular control over your show/hide behavior.
这篇关于Django Admin:需要有条件地显示字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!