Django:一个 modelChoiceField 依赖于另一个 modelChoiceField

Django: a modelChoiceField depend on another modelChoiceField

我有两个用数据库填充的 ModelChoiceFields:

t1=MyModelChoiceField1(queryset=operation_Bancaire.objects.all(),to_field_name='type_tiers',required=False)
t2=MyModelChoiceField2(queryset=operation_Bancaire.objects.all(),required=False)

我需要让 t2 依赖于 t1 。 例如 : 如果我在 t1 中选择 value1,t2 仅显示在第一个 ModelChoiceField t1

中选择的 value1 过滤的值

我对此进行了很多搜索,发现了一些 Ajax 可以帮助我这样做的信息。但我不确定它是否会让我控制我的 ModelChoiceFields,因为我特别希望我的 ChoiceFields 从数据库中填充,因为我从未使用过 Ajax。 请提供任何帮助,这样我就可以正确地实现我所需要的。

谢谢。

你说得对,ajax/javascript 是最好的方法。 django 中没有内置的方法来动态地执行此操作。 This link 是迄今为止最全面地解释了如何在 django 中实现 ajax。