I have a form that handles m2m request in the validation.
my form looks as follows
class HuntingReportForm(ModelForm):
date_travel_started = forms.DateField(widget=extras.SelectDateWidget(years=range(1970,2010)))
date_travel_ended = forms.DateField(widget=extras.SelectDateWidget(years=range(1970,2010)))
wish_list = forms.ModelMultipleChoiceField(queryset=Specie.objects.all(), widget=FilteredSelectMultiple("verbose name", is_stacked=False), required=False)
bag_list = forms.ModelMultipleChoiceField(queryset=Trophies.objects.all(), widget=FilteredSelectMultiple("verbose name", is_stacked=False), required=False)
class Meta:
model = HuntingReport
exclude = ['user',]
def __init__(self, user, *args, **kwargs):
self.validate = kwargs.pop('validate',False)
super(HuntingReportForm, self).__init__(*args, **kwargs)
users = User.objects.filter(userprofile__outfitter=True)
self.fields['outfitter'].choices = [('', '')] + [(user.pk, user.get_full_name()) for user in users]
self.fields.keyOrder = ['title', 'report', 'date_travel_started', 'date_travel_ended', 'wish_list', 'bag_list', 'outfitter']
When I remove the whole def __init__ section the forms seems to validate and save, however with the following code it just keep throwing me back to the page saying that some fields weren’t filled in, even when they were.
Any ideas?
You don’t show the view, but presumably you’re instantiating the form with the usual
form = HuntingReportForm(request.POST). Unfortunately, you’ve changed the signature of the form’s__init__method to expectuseras the first positional argument, so the data isn’t being passed in. You should moveuserto kwargs and treat it the same as yourvalidateargument: