Django ModelChoiceField:过滤查询集并将默认值设置为对象 [英] Django ModelChoiceField: filtering query set and setting default value as an object
问题描述
我有一个Django表单类定义喜欢这个在模型
:
class AccountDetailsForm(forms.Form):
...
adminuser = forms.ModelChoiceField(queryset = User.objects.all())
这可以正常工作,但是有一些限制我似乎无法解决:
1)我想使用过滤器在查询器上,基于传递给表单的变量 accountid
,如下所示:
User.objects.filter(account = accountid)
这不能在模型中工作,因为 accountid
不能作为变量传递。
因此,查询集
必须以某种方式在视图
中定义,但据我看到它是(2)我想默认选择一个 AccountDetailsForm
一个对象在数据库中,我可以选择在视图
中,如下所示:
User.objects.filter = accountid).filter(primary_user = 1)
我尝试将adminuser指定为默认值(与其他标准表单字段一起使用,如 CharField
):
adminuser = User.objects.filter(account = accountid).filter(primary_user = 1)
...
form = AccountDetailsForm({'adminuser' :adminuser})
返回render_to_response('accounts / edit / accountdetails.html',
{'form':form,'account':account})
但没有运气。
我应该使用除给我这里需要的灵活性的ModelChoiceField
谢谢。
覆盖init方法并接受一个新的关键字参数
class AccountDetailsForm(forms.Form):
...
adminuser = forms.ModelChoiceField(queryset = User.objects.all())
def __init __(self,* args,** kwargs):
accountid = kwargs.pop('accountid',None)
super(AccountDetailsForm,self).__ init __(* args,** kwargs)
如果accountid:
self.fields ['adminuser']。queryset = User.objects.filter (account = accountid)
form = AccountDetailsForm(accountid = 3)
您可以随时在视图中手动设置选项。
form = AccountDetailsForm()
form.fields ['adminuser']。queryset = User.objects.filter(account = accountid)
请注意:您不是通过将字典传递给您的示例中的表单来设置默认值。
您实际上正在创建一个绑定表单,可能触发验证和所有爵士乐。
要设置默认值,使用首字母参数
form = AccountDetailsForm(initial = {'adminuser':'3'})
I have a Django Form class defined likes this in Models
:
class AccountDetailsForm(forms.Form):
...
adminuser = forms.ModelChoiceField(queryset=User.objects.all())
This works OK, but it has some limitations I can't seem to work around:
(1) I would like to use a filter on the queryset, based on a variable accountid
passed to the form, like this:
User.objects.filter(account=accountid)
This can't work in the model because accountid
can't be passed as a variable, of course.
It follows that the queryset
must somehow be defined in the Views
, but as far as I can see it's a required field in the Form class.
(2) I would like to make the default choice of AccountDetailsForm
an object in the database, which I can select in the Views
like this:
User.objects.filter(account=accountid).filter(primary_user=1)
I've tried specifying the adminuser as a default value in the form, (which works with other standard form fields, like CharField
):
adminuser = User.objects.filter(account=accountid).filter(primary_user=1)
...
form = AccountDetailsForm({'adminuser': adminuser})
return render_to_response('accounts/edit/accountdetails.html',
{'form': form, 'account':account})
But no luck.
Should I be using something other than ModelChoiceField
given the flexibility I need here?
Thanks.
Override the init method and accept a new keyword argument
class AccountDetailsForm(forms.Form):
...
adminuser = forms.ModelChoiceField(queryset=User.objects.all())
def __init__(self, *args, **kwargs):
accountid = kwargs.pop('accountid', None)
super(AccountDetailsForm, self).__init__(*args, **kwargs)
if accountid:
self.fields['adminuser'].queryset = User.objects.filter(account=accountid)
form = AccountDetailsForm(accountid=3)
You can always just set the choices manually in the view as well.
form = AccountDetailsForm()
form.fields['adminuser'].queryset = User.objects.filter(account=accountid)
Be warned: you are not setting default values by passing in a dictionary to a form like in your example.
You are actually creating a Bound Form, potentially triggering validation and all that jazz.
To set defaults, use the initials argument.
form = AccountDetailsForm(initial={'adminuser':'3'})
这篇关于Django ModelChoiceField:过滤查询集并将默认值设置为对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!