Efrin
Efrin

Reputation: 2423

Django - calling super in form __init__ starts form validation on "GET"

I have a ModelForm in my application in which I want to modify init function to add some customisation.

When init is commented out then the form works and validates properly. When I override init and go to url where the form is rendered it automatically says that "Field xyz is required"

Whats the cause of that problem?

class CreateListView(FormMixin, ListView):

    def get_context_data(self, **kwargs):
        self.object_list = self.get_queryset()
        data = super(ListView, self).get_context_data()
        data['object_list'] = self.get_queryset()
        data['form'] = self.get_form(self.form_class)
        return data

    def post(self, request, *args, **kwargs):
        form = self.form_class(request.POST)
        if form.is_valid():
            form = form.save()
            return HttpResponseRedirect(form.get_absolute_url())
        return self.form_invalid(self.get_context_data())



class ActionGroupForm(forms.ModelForm):

    class Meta:
        model = ActionGroup

    def __init__(self, *args, **kwargs):
        super(ActionGroupForm, self).__init__(args, kwargs)

Upvotes: 1

Views: 784

Answers (1)

falsetru
falsetru

Reputation: 369494

You are missing *, **:

super(ActionGroupForm, self).__init__(*args, **kwargs)

Upvotes: 6

Related Questions