Django:从视图设置字段选择吗? [英] Django: Set field choices from view?

查看:67
本文介绍了Django:从视图设置字段选择吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些< selects> ,我需要填充一些选择,具体取决于当前登录的用户。我不是认为在表单类内部可以做到(或很容易做到),所以我可以将选项保留为空白并在视图中进行设置吗?还是应该采取什么方法?

I've got some <selects> that I need to populate with some choices that depend on the currently logged in user. I don't think this is possible (or easy) to do from inside the form class, so can I just leave the choices blank and set them in the view instead? Or what approach should I take?

推荐答案

不确定这是否是最好的答案,但是过去我已经选择了在表单的 init 中选择字段-您可以将选择传递给表单的构造函数...

Not sure if this is the best answer, but in the past I have set the choices of a choice field in the init of the form - you could potentially pass your choices to the constructor of your form...

这篇关于Django:从视图设置字段选择吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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