Modal Multiplayer does not select the preliminary options and I can not work on my following example (link below):
/ P >
My models and form:
class company (models.Model): company_name = models.CharField (max_length = 200) class contact (model model) : Company = Model.Argink's (Company) First_Name = Model.Charfild (max_long = 100) Last_Name = Model. Richfield (max_long = 100) Class action (model.model): company = m Class Action_Form (ModelForm): _company = models.ManyToManyField (contact, verbose_name = '' participant (')' from company, empty = true, zero = true) from Araginki (Company, empty = true, zero = true) What do I do and the results:
P>
>>> contacts_from_company = Contact.objects.filter (Cama Ny__exact = 1) # 1 for testing, otherwise one variable >>> Form = Action_Form (initial = {'to-company': [o.pk for Contacts_from_company}} fixed in the form of # >> Suggestion in print form ['to_company'] & lt; Ul & gt; & Lt; / Ul & gt; >>> Print Contact_light_company [& lt; Contact: test person & gt;, & lt; Contact: The other person & gt;] >>> form2 = Action_Form (initial = {'from_company': contacts_from_company}) >>> print form2 ['from_company'] & lt; Ul & gt; & Lt; / Ul & gt; >>> form3 = Action_Form (initial = {'from_company': contact.objects.all ()}) >>> Print form 3 ['from_company'] & lt; Ul & gt; & Lt; / Ul & gt;
The way I was expecting it would work:
1. Get the "My" view from the request. GET. 2. After that, it filters all "contacts" for the "company"
3. Finally, it creates a form and "contacts" in the form of "initial = {...}"
Two questions:
1 [Not answered yet] How can I do ModelMultipleChoiceField those "Initial" Take it?
2 [Answered] As an alternative, can I pass a variable in Action_Form so that I have in my model form:
from_company = forms.ModelMultipleChoiceField (queryset = contact.objects.filter (Company__exact = some_id) # where some AD comes from a view
To set a your initial values, set
call to __int__ method to __init __
to base ModelForm
via super < From the action_form
/ Strong>.
class Action_Form (forms.ModelForm): def __init __ (auto, * args, ** kwargs): super (Action_Form, itself) .__ init (__ (* Args, ** kwargs) Self-fields ['from_company']. Queryset = Contact.object.filter (...
If you set your filter parameter to Action_Form
Before you will need to delete them before Super:
myfilter = kwargs ['myfilter'] del kwargs ['myfilte r']
Or, maybe better:
myfilter = kwargs.pop ('myfilter')
For more information, another link is referenced here .
Comments
Post a Comment