ModelMultipleChoiceField不选择初始选择

ModelMultipleChoiceField不选择初始选择

本文介绍了Django:ModelMultipleChoiceField不选择初始选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ModelMultipleChoiceField不会选择初始选择,我无法在我的示例中进行以下修复(下面的链接):

ModelMultipleChoiceField doesn't select initial choices and I can't make the following fix (link below) work in my example:

我的模型和形式:

class Company(models.Model):
    company_name = models.CharField(max_length=200)

class Contact(models.Model):
    company = models.ForeignKey(Company)
    first_name = models.CharField(max_length=100)
    last_name = models.CharField(max_length=100)

class Action(models.Model):
    company = models.ForeignKey(Company, blank=True, null=True)
    from_company = models.ManyToManyField(Contact, verbose_name='Participant(s) from "Company"', blank=True, null=True)

class Action_Form(ModelForm):
    from_company = forms.ModelMultipleChoiceField(queryset=Contact.objects.none(), widget=forms.CheckboxSelectMultiple())
    class Meta:
        model = Action

我做的和结果:


>>> contacts_from_company = Contact.objects.filter(company__exact=1) # "1" for test, otherwise a variable
>>> form = Action_Form(initial={'from_company': [o.pk for o in contacts_from_company]}) # as suggested in the fix
>>> print form['from_company']
<ul>
</ul>
>>> print contacts_from_company
[<Contact: test person>, <Contact: another person>]

>>> form2 = Action_Form(initial={'from_company': contacts_from_company})
>>> print form2['from_company']
<ul>
</ul>

>>> form3 = Action_Form(initial={'from_company': Contact.objects.all()})
>>> print form3['from_company']
<ul>
</ul>

我希望它的工作方式:

1.我的观点从request.GET获取公司

2.然后过滤该公司的所有联系人

3.最后,它创建一个表单,并将这些联系人传递为initial = {... }

The way I was hoping it would work:
1. My view gets "company" from request.GET
2. It then filters all "contacts" for that "company"
3. Finally, it creates a form and passes those "contacts" as "initial={...}"

两个问题:

1。 [尚未回答] 如何使ModelMultipleChoiceField采取这些初始值?

2。 [回答] 作为替代,我可以将一个变量传递给Action_Form(ModelForm),以便在我的ModelForm中可以有:

Two questions:
1. [not answered yet] How can I make ModelMultipleChoiceField take those "initial" values?
2. [answered] As an alternative, can I pass a variable to Action_Form(ModelForm) so that in my ModelForm I could have:


from_company = forms.ModelMultipleChoiceField(queryset=Contact.objects.filter(company__exact=some_id) # where some_id comes from a view


推荐答案

您需要向 Action_Form 添加一个 __ init __ 方法来设置初始值记住通过 super 调用基础 ModelForm class __ init __

You will need to add an __init__ method to Action_Form to set your initial values, remembering to call __init__ on the base ModelForm class via super.

class Action_Form(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(Action_Form, self).__init__(*args, **kwargs)
        self.fields['from_company'].queryset = Contact.object.filter(...

如果您计划将过滤器参数作为关键字参数传递给 Action_Form ,则需要在调用super之前删除它们:

If you plan to pass your filter params as keyword args to Action_Form, you'll need to remove them prior invoking super:

myfilter = kwargs['myfilter']
del kwargs['myfilter']

或者可能更好:

myfilter = kwargs.pop('myfilter')

有关更多信息,请参阅。

For more information, here's another link referring to Dynamic ModelForms in Django.

这篇关于Django:ModelMultipleChoiceField不选择初始选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 02:43