Django formset - 根据用户cookie验证输入?

时间:2017-12-14 15:15:20

标签: django forms validation django-forms formsets

我有一个包含单个字段数量的Django表单(TestForm)。我还有一个Django formset(TestFormset),它包含我的TestForm的多个实例。

我想为我的TestFormset编写一个自定义的clean()方法,该方法验证我的多个TestForms中指定的数量总和等于存储在会话变量中的数字max_quantity。

我知道我可以在views.py中执行此验证(例如,在我的formset经过验证和清理后,我可以手动总结我的TestForms中的数量变量并检查到确保它们等于request.session [' max_quantity'],如果发现任何问题则抛出错误。)

但理想情况下,我喜欢将所有表单验证逻辑移到forms.py的clean()方法中。但是,我无法弄清楚如何将外部值传递到我的Formset中,而该Formset没有链接到其单个表单之一。

这可能吗?

forms.py

from django.forms import BaseFormSet

class TestForm(forms.Form):
    quantity = forms.IntegerField()

class BaseTestFormset(BaseFormset):
    def clean(self):
        if any(self.errors):

            # Don't bother validating the formset unless each form is valid on its own

            return

        quantity = 0

        for form in self.forms:
            quantity += form.cleaned_data['quantity']

        # IF QUANTITY IS NOT EQUAL TO MAX_QUANTITY, THROW AN ERROR...
        # ...BUT HOW DO WE GET THE MAX_QUANTITY INTO THIS FUNCTION?

views.py

from .forms import TestForm, BaseTestFormset

def serve_form(request):

    TestFormSet = formset_factory(TestForm, formset=BaseTestFormset)

    if request.method == 'POST':
        formset = TestFormSet(request.POST)

        # This method should check to ensure that the sum of quantities within our formsets does not exceed max_quantity
        if formset.is_valid():
              # Proceed to take action
    else:
        # Sample initial data
        formset = TestFormSet(initial=[{'quantity': 5}, {'quantity': 7}])

    # I CAN PASS MAX_QUANTITY INTO THE TEMPLATE... BUT HOW DO I GET IT INTO THE FORMSET VALIDATION METHOD?
    return render(request, 'template.html', {'formset': formset, 'max_quantity': request.session['max_quantity']}

1 个答案:

答案 0 :(得分:1)

与表单一样,如果要访问某个方法中的某些内容,则需要在某个位置传递它。如果您愿意,可以在初始化程序中执行此操作:

class BaseTestFormset(forms.BaseFormSet):
    def __init__(self, *args, **kwargs):
        self.max_quantity = kwargs.pop('max_quantity', None)
        super(BaseTestFormset, self).__init__(*args, **kwargs)

    def clean(self):
        ...
        if quantity > self.max_quantity:
            ...

并在视图中:

if request.method == 'POST':
    formset = TestFormSet(request.POST, max_quantity=request.session['max_quantity'])