djangodjango-formsdjango-validation

How can I validate form when `CreateWithInlinesView` from the Django Extra Views is used?


def clean(self) and def clean_name_of_the_field methods don't work there. I have checked it on function based view , so that they really don't work in CreateWithInlinesView. I could still use validators and def clean() in model, but I would prefer to do it in forms...


Solution

  • This can be done in the views.
    I found a solution here

    class ModelCreate(CreateWithInlinesView):
        model = models.Model
        inlines = [ModelInline]
        form_class = forms.ModelForm
        success_url = reverse_lazy("app:related_name")
    
        def forms_valid(self, form, inlines):
            self.object = form.save()
            """Write your extra code in here"""
            return HttpResponseRedirect(self.get_success_url())