16๐
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(...
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']
or, probably better:
myfilter = kwargs.pop('myfilter')
For more information, hereโs another link referring to Dynamic ModelForms in Django.
23๐
Iโm replying for 1)
1. How can I make ModelMultipleChoiceField take those โinitialโ values?
This could be done in your Action_Form
__init__
method using ModelMultipleChoiceField initial
attribute.
As it says in the Django source code (db/models/fields/related.py)
in def formfield(self, **kwargs)
:
# If initial is passed in, it's a list of related objects, but the
# MultipleChoiceField takes a list of IDs.
So you need to give it a list of IDs:
class Action_Form(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(Action_Form, self).__init__(*args, **kwargs)
self.fields['from_company'].initial = [c.pk for c in Contact.object.filter()]
- [Django]-Are sessions needed for python-social-auth
- [Django]-How can I run a celery periodic task from the shell manually?
- [Django]-Raw_id_fields: How to show a name instead of id?
13๐
If previous answer wasn't straight-forward enough, I try to answer 1) again:
- How can I make ModelMultipleChoiceField take those "initial" values?
You can leave Action_Form
as it was in the original question, and just use this to render exactly what you want:
>>> form4 = Action_Form(initial={'from_company': Contact.objects.all().values_list('id',flat=True)})
>>> print form4['from_company']
- [Django]-Upload image available at public URL to S3 using boto
- [Django]-Missing Table When Running Django Unittest with Sqlite3
- [Django]-How to access request body when using Django Rest Framework and avoid getting RawPostDataException
12๐
Answer to (1) question!
This will not work:
self.fields['from_company'].initial = [c.pk for c in Contact.object.filter()]
But this will really work:
self.initial['from_company'] = [c.pk for c in Contact.object.filter()]
- [Django]-Renaming an app with Django and South
- [Django]-'Request header field Authorization is not allowed' error - Tastypie
- [Django]-Checking the number of elements in an array in a Django template
0๐
The problem is because the Contact.objects.all().values_list('id', flat=True)
if a QuerySet object, the MultipleChoiceField needs a list, see in the doc
To fix this, you need to convert the QuerySet to a list!
Exempla:
contacts = list(Contact.objects.values_list('id', flat=True))
form = Action_Form({'from_company': contacts})
- [Django]-Django 1.8 KeyError: 'manager' on relationship
- [Django]-Redirect to a url with query string in Django
- [Django]-Django, global template variables