我使用了django内联formset,所以在两个模型上做一个单一的crud . 与django表单不同, django formset 一次使用多个表单 . 所以我真的很困惑我验证django formset . 但我需要验证我的django formset . 我真的还没有找到任何解决方案 .
我有两个模型,即 Discount 和 Discount products . 我使用了 discount products formsets 的 discount products formsets 的django formset .
默认情况下,formsets允许用户在创建新实例或更新新实例时避免所有表单,这实际上是有意义的,因为可以有15或20个表单 . 在我的系统中,我也遵循相同的方式 .
在我的 discount formsets 中,有两个字段,即 primary variant
和 variants
.
如果用户没有填写任何形式的表单集的任何字段,他可以这样做并且只能创建或更新 discount form 对象,这很好 . (记得 discount form 位于 discount products formsets 之上) .
但如果他只填写 variant
字段并且显示 primary variant
字段,则用户不应该允许这样做,因为在我的系统中,如果没有 primary variant
,则无法创建或更新 variant
. 但是在这一点上我无法验证它 . 因为它是一个formsets . 我真的需要知道如何进行这种类型的验证 .
这是他 Create view 我在哪里组合了 discount 和 discount products . (只提供创建视图,我希望解决方案对于更新视图也是一样的)
class DiscountCreate(RequestPassingFormViewMixin, WammuCreateView):
model = Discount
template_name = 'dashboard/discount_form.html'
form_class = DiscountForm
def get_form_kwargs(self, *args, **kwargs):
kwargs = super(DiscountCreate, self).get_form_kwargs(*args, **kwargs)
common = get_object_or_404(Common, pk=self.kwargs['common_pk'])
discount = Discount(common=common)
kwargs['instance'] = discount
return kwargs
def get(self, request, *args, **kwargs):
self.object = None
form_class = self.get_form_class()
form = self.get_form(form_class)
DiscountProductInlineFormSet = inlineformset_factory(Discount, DiscountProducts,
form=DiscountProductForm,
can_delete=True, extra=5)
DiscountProductInlineFormSet.form = staticmethod(curry(DiscountProductForm, request=request))
discount_product_formset = DiscountProductInlineFormSet(instance=self.object)
return self.render_to_response(
self.get_context_data(form=form, discount_formset=discount_product_formset))
def post(self, request, *args, **kwargs):
self.object = Discount()
form = self.get_form(self.form_class)
DiscountProductInlineFormSet = inlineformset_factory(Discount, DiscountProducts,
form=DiscountProductForm,
can_delete=True, extra=5)
DiscountProductInlineFormSet.form = staticmethod(curry(DiscountProductForm, request=request))
if form.is_valid():
self.object = form.save(commit=False)
else:
discount_product_formset = DiscountProductInlineFormSet(request.POST,
instance=self.object)
return self.render_to_response(
context=self.get_context_data(form=form, discount_formset=discount_product_formset))
promotion_product_formset = DiscountProductInlineFormSet(request.POST, instance=self.object)
if discount_product_formset.is_valid():
self.object.save()
discount_product_formset.save()
return super(Discountreate, self).form_valid(form)
else:
return self.render_to_response(
context=self.get_context_data(form=form, discount_formset=discount_product_formset))
这是 Discount 和 Disount Products 所在的表格,
class DiscountForm(RequestPassingFormMixin, ModelForm):
class Meta:
model = Discount
def __init__(self, *args, **kwargs):
super(PromotionForm, self).__init__(*args, **kwargs)
common = Chain.objects.get(pk=self.request.session.get('chain_pk'))
class DiscountProductForm(autocomplete_light.ModelForm):
class Meta:
model = DiscountProducts
fields = ['code', 'variants', 'primary_variant']
def __init__(self, *args, **kwargs):
self.request = kwargs.pop("request")
super(DiscountProductForm, self).__init__(*args, **kwargs)
common = Chain.objects.get(pk=self.request.session.get('common_pk'))
self.fields['variants'].queryset = Variant.objects.filter(product__common__pk=common.pk)
self.fields['primary_variant'].queryset = Variant.objects.filter(product__common__pk=common.pk)
3 回答
当我遇到你的问题时,这是我的解决方案,在你的views.py中
如果至少有一个字段不为空,则验证formset中的字段,因此默认情况下一切都应该按预期工作 . 您还可以阅读cleaning and validating fields that depend on each other .
只需将参数
min_num=1
和validate_min=True
添加到inlineformset_factory