I am trying to load default values for a MultipleChoiceField in a Form overload.
So if I give the initial variable inside the MultipleChoiceField it works:
class UserPreferences(forms.Form):
my_form = forms.MultipleChoiceField(
widget = forms.CheckboxSelectMultiple(),
choices = MY_CHOICES,
initial = MY_INITIAL_DICT)
But if I try to get initial values from the database related to a user and update initial as follows, my_form initial variable doesn't change and stays empty:
class UserPreferences(forms.Form):
my_form = forms.MultipleChoiceField(
widget = forms.CheckboxSelectMultiple(),
choices = MY_CHOICES)
def __init__(self, user):
initial = make_my_dict_from( MyModel.objects.filter(user=user) )
super(UserPreferences, self).__init__(initial, empty_permitted)
If I do this with a simple ChoiceField it works, but not with MultipleChoiceField.
Thanks for any suggestions!
try this:
super(UserPreferences, self).__init__(initial=initial)
You need to use keyword arguments like this. Python does not look at your variable names, and uses the right slots.
Where does "empty_permitted" come from? Your example does use, but not define this variable.
Related
I have a little problem with a dropdown field in django form. I want to add to this field placeholder (or not active first option as placeholder) and some other stuff as no label, class etc.
I wrote something like this, in my forms.py but now my form is broken - don't save values to database.
from .models import Scenario, ScenarioArea
from django import forms
class newScenario(forms.ModelForm):
scenarioArea=forms.ModelChoiceField(label="", queryset=ScenarioArea.objects.values_list("scenarioAreaName", flat=True).distinct(), empty_label=None)
lass Meta:
model = Scenario
fields = ['scenarioArea']
Every other field type is working, but not this dropdown...
Could you help me?
By default the widget used by ModelChoiceField will have an
empty choice at the top of the list. You can change the text of this
label (which is "---------" by default) with the empty_label
attribute, or you can disable the empty label entirely by setting
empty_label to None:
As you're setting empty_label to None, this option is not being shown.
Example
scenario_area = forms.ModelChoiceField(label="", queryset=ScenarioArea.objects.distinct('scenarioAreaName'), empty_label="Placeholder")
I'm working with Python2 and Django 1.9.
Basically I have a form that contains two dropdowns. The second dropdown depends on the value of the first one.
For exampe, if dropdown #1 has the option "Category" selected, then dropdown #2 should display options "CategoryA, CategoryB and CategoryC". In the same way, if dropdown#1 has the option "Department" selected, Dropdown#2 should show "Department1, Department2, Department3".
Notice that both "Departments" and "Categories" are classes with their corresponding database tables.
So here comes my question. How to define that form? More especifcally, how do I indicate that the second form will sometimes display objects from the class Category and sometimes objects of the class Department?
This is what I have so far:
class MyClassForm(forms.Form):]
name = forms.CharField(max_length=255)
dropdown1 = forms.ModelChoiceField(
queryset=TypeOfCriteria.objects.all().order_by('name'))
dropdown2 = forms.ModelChoiceField(
queryset=Department.objects.none())
Notice how I've defined dropdodown2:
dropdown2 = forms.ModelChoiceField(
queryset=Department.objects.none())
How should I define the value of the parameter queryset for dropdown2? Since I have to specify the class that is going to be queried to obtain the list of all of its instances, how should I do it?
Right now, I'm loading the content of dropdown2 with JQuery. But when I hit the "send" button to send the post data I'm always getting the error: "Select a valid choice. That choice is not one of the available choices."
One option would be to update the queryset dynamically in the __init__ method of the form. Keep the rest of your form class as is, then add this code:
def __init__(self, *args, **kwargs):
super(MyClassForm, self).__init__(*args, **kwargs)
if 'dropdown1' in self.data:
self.fields['dropdown2'].queryset = Department.objects.filter(typeofcriteria=self.data['dropdown1'])
In the init method
def __init__(self, *arts, **kwargs):
super(MyClassForm, self).__init__(*args, **kwargs)
self.fields['dropdown2'].queryset = Department.objects.none()
if self.is_bound:
self.fields['dropdown2'].queryset = Department.objects.filter(# any value in self.data)
In my application I have a CreateView that must initialize some fields of the model with a default value, different from the default defined inside the model.
I do not want the user to edit the value, thus I put the field in the exclude list
class AForm(ModelForm):
class Meta:
model = AModel
exclude = ['a_field']
class AView(CreateView):
form_class = AForm
The question is: where do I set the value of a_field?
I tried to define clean methods inside AForm, like thus
class AForm(ModelForm):
[...]
def clean(self):
d = super(AForm, self).clean()
d['a_field'] = 10
return d
however a_field is set to whatever its default value defined in the model is, rather than 10. I also tried defining clean_a_field, but that is simply not executed.
If I remove a_field from the exclude list, then the clean and clean_a_field will work, but the form won't validate unless I render some <input name="a_field"> inside the template, which is not optimal.
I managed to solve the issue in a way that makes me satisfied, although I'm still not 100% happy with the code.
a_field is a required (by the model) field, thus it is necessary to render an <input name="a_field"> inside the template. The trick was to make a_field non-required:
class AForm(ModelForm):
a_field = Field(required=False,
widget=forms.HiddenInput)
class Meta:
model = AModel
def clean_a_field(self):
return 10
This way I can avoid rendering the field in my template, and the form will still validate. And even if the form is rendered with {{ form.as_p }}, widget=forms.HiddenInput saves my day.
Exclude the field from the form, then in the view you can set the value before you save the form:
form = AForm(request.POST)
if form.is_valid():
new_record = form.save(commit=False)
new_record.a_field = 10
new_record.save()
You also might want to avoid the exclude list and specify which fields you'd like to include with the fields attr of the form definition. See the first paragraph here in the docs.
You set a default value in the model. From the official document,
a_field = models.CharField(max_length=7, default=''), for example
I have a way to Face this situation. Follow the following process:
Remove 'a_field' from the excludes in AForm.
Do not expose 'a_field' in HTML template. i.e. Don't give the user option to change the value via Form in Template. This would ensure that normal user's wont modify the value.
To prevent completely, over-ride get_form_kwargs in the View.
This would provide or over-ride your desired value to 'a_field' and save that
e.g.
class AView(CreateView):
form_class = AForm
def get_form_kwargs(self):
kwargs = super(AView, self).get_form_kwargs()
if self.request.method in {'POST', 'PUT'}:
# Change post data to over-ride or provide value of 'a_field'
data = self.request.POST.copy()
data.update({
'a_field': 'value'
})
kwargs['data'] = data
return kwargs
I have a wtforms form
class MyForm(Form):
names = SelectField('name', choices=[])
The choices of names field is built dynamically and rendered in the template by an ajax call. When I submit the form, it raises an exception "not a valid choice". I don't want the form to validate the choices of names field for me. How can I disable the validation?
I did something like this to step around the SelectMultipleField validation in WTForms. It should work the same way with a plain SelectField
class NonValidatingSelectMultipleField(SelectMultipleField):
"""
Attempt to make an open ended select multiple field that can accept dynamic
choices added by the browser.
"""
def pre_validate(self, form):
pass
I simply override the built-in validation.
I was stuck with the same issue. The solution provided by Xealot is great. I found that there is an option to set validation to False using validate_choice=False. I have included an example of both the solutions below.
class NonValidatingSelectField(SelectField):
"""
Attempt to make an open ended select multiple field that can accept dynamic
choices added by the browser.
"""
def pre_validate(self, form):
pass
class MyForm(Form):
names = NonValidatingSelectField('name')
names2 = SelectField('name2', validate_choice=False)
By "I don't want the form to validate the choices", I assume you actually mean "I'm going to do it myself later and doubt the form's ability to do it correctly".
But you are in luck! You can subclass an existing form to add choices dynamically.
class MyForm(Form):
# other fields...
def some_handler(request):
name_choices = build_name_choices()
class RealForm(MyForm):
names = SelectField('name', choices=name_choices)
form = RealForm(request.GET)
form.validate()
This also saves you from the tedium of merging the form's validation and error messages with those you generate yourself later.
I am creating a form that uses MultipleChoiceField. The values for this field are derived from another model. This method works fine, however, I am noticing (on the production server) that when I add a new item to the model in question (NoticeType), the form does not dynamically update. I have to restart the server for the new item to show up on my MultipleChoiceField.
Any changes to the NoticeType model (editing items or creating new ones) do not propagate to the form. After I restart the production server, the updates appear.
Any ideas why this might be ? The relevant portion of the form is below. Thanks.
from django import forms
from django.contrib.auth.models import User
from notification.models import NoticeType
class EditUserProfileForm(forms.Form):
CHOICES = []
for notice in NoticeType.objects.all():
CHOICES.append( (notice.label,notice.display) )
notifications = forms.MultipleChoiceField(
label="Email Notifications",
required=False,
choices=( CHOICES ),
widget=forms.CheckboxSelectMultiple,)
Although mherren is right that you can fix this problem by defining your choices in the __init__ method, there is an easier way: use the ModelMultipleChoiceField which is specifically designed to take a queryset, and updates dynamically.
class EditUserProfileForm(forms.Form):
notifications = forms. ModelMultipleChoiceField(
label="Email Notifications",
required=False,
queryset = NoticeType.objects.all(),
widget=forms.CheckboxSelectMultiple)
My hunch is that the class definition is only being processed once on load rather than for each instantiation. Try adding the CHOICES computation to the init method like so:
def __init__(self, *args, **kwargs):
super(self.__class__, self).__init__(*args, **kwargs)
CHOICES = []
for notice in NoticeType.objects.all():
CHOICES.append( (notice.label, notice.display) )
self.fields['notifications'].choices = CHOICES