modelmultiplechoicefield

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

how to receive modelform_instance.cleaned_data['ManyToMany field'] in view when form field is ModelMultipleChoiceField?

天涯浪子 提交于 2019-12-13 02:42:54
问题 Here is the situation: I have a model as below: class School(Model): name = CharField(...) Permit model has three objects: School.objects.create(name='school1') # id=1 School.objects.create(name='school2') # id=2 I have another model: Interest(Model): school_interest = ManyToManyField(School, blank=True,) I then build a ModelForm using Interest: class InterestForm(ModelForm): school_interest = ModelMultipleChoiceField(queryset=School.objects.all(), widget=CheckboxSelectMultiple, required