【问题标题】:CreateWithInlinesView validation in form表单中的 CreateWithInlinesView 验证
【发布时间】:2019-09-03 09:00:00
【问题描述】:
有谁知道在使用 Django Extra Views 中的CreateWithInlinesView 时如何验证表单?
令人惊讶的是,def clean(self) 和 def clean_name_of_the_field 方法在那里不起作用。我已经在基于函数的视图上检查了它,所以它们在CreateWithInlinesView 中确实不起作用。我仍然可以在模型中使用验证器和def clean(),但我更愿意在表单中使用...
如果您在CreateWithInlinesView 验证方面有任何经验,请告诉我。
谢谢!
【问题讨论】:
标签:
django
django-forms
django-validation
【解决方案1】:
我知道这已经晚了,但我也遇到了同样的问题。我刚刚添加了一些内容来帮助理解。这可以在视图中完成。
我找到了解决方案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())