multiplechoicefield

Reload choices dynamically when using MultipleChoiceFilter

你离开我真会死。 提交于 2020-01-03 09:52:30
问题 I am trying to construct a MultipleChoiceFilter where the choices are the set of possible dates that exist on a related model ( DatedResource ). Here is what I am working with so far... resource_date = filters.MultipleChoiceFilter( field_name='dated_resource__date', choices=[ (d, d.strftime('%Y-%m-%d')) for d in sorted(resource_models.DatedResource.objects.all().values_list('date', flat=True).distinct()) ], label="Resource Date" ) When this is displayed in a html view... This works fine at

Changing required field in form based on condition in views (Django)

大城市里の小女人 提交于 2019-12-12 10:06:56
问题 I have a form, LabelingForm() with two multiplechoicefields and I wish to set the required - parameter so that it is False when pressing button A and B but True when pressing button C. I 've tried with initial = False and required = True but it doesn't work, it requires field choice when pressing button A. in forms.py class LabelingForm(forms.Form): First_choices = (('1',''), ..... ) First_choice = forms.MultipleChoiceField(choices=First_choices, initial=True,required=True) Second__choices =

django ModelMultipleChoiceField set initial values

て烟熏妆下的殇ゞ 提交于 2019-12-10 01:23:29
问题 I have the following code: category = forms.ModelMultipleChoiceField( label="Category", queryset=Category.objects.order_by('name'), widget=forms.Select( attrs={ 'placeholder': 'Product Category', 'class': 'form-control'}), required=True ) how do I set an initial value in the select box like "Choose a category" so that the select box should have a list of categories with the initial value being "Choose a category" 回答1: If you pass a QuerySet object as an initial value and if widget=forms

django ModelMultipleChoiceField set initial values

雨燕双飞 提交于 2019-12-04 22:48:06
I have the following code: category = forms.ModelMultipleChoiceField( label="Category", queryset=Category.objects.order_by('name'), widget=forms.Select( attrs={ 'placeholder': 'Product Category', 'class': 'form-control'}), required=True ) how do I set an initial value in the select box like "Choose a category" so that the select box should have a list of categories with the initial value being "Choose a category" If you pass a QuerySet object as an initial value and if widget=forms.CheckboxSelectMultiple , then the check boxes are not checked. I had to convert the QuerySet object to a list,