提升forms.ValidationError不起作用

时间:2012-06-03 01:29:38

标签: django validation django-forms inline-formset

我有以下型号:

class Project(models.Model):
    title = models.CharField(max_length="100")

    pub_date = models.DateField(auto_now_add=True, editable=False)

    budget = models.IntegerField()

class Milestone(models.Model):

    title = models.CharField(max_length="50")

    budget_percentage = models.IntegerField(max_length=2)

    project = models.ForeignKey(Project)

在项目的创建表单中,我已经包含了里程碑的内联表单集。

我想验证,当项目提交时,至少创建了4个里程碑,并且所有里程碑的budget_percentage总计达到100

这是我的表格:

class BaseMilestoneProjectFormSet(BaseFormSet):
    def clean(self):

        if any(self.errors):
            # Don't bother validating the forms unless each form is valid on its own
            return

        if len(self.forms) < REQUIRED_MILESTONES:
            raise forms.ValidationError("At least %s milestones need to be created" % REQUIRED_MILESTONES)

        # Set initial control variables
        # Total percentage of budget to control
        total_percentage = 0
        # Date to control that milestones are linear, i.e. that second milestone isn't delivered before first
        current_control_date = date.min

        for i, form in zip(range(len(self.forms)), self.forms):

            if i == 0 and form.budget_percentage > MAX_BUDGET_FIRST_MILESTONE:
                raise forms.ValidationError("First milestone budget must not exceed %s percentage" % MAX_BUDGET_FIRST_MILESTONE)
            elif form.budget_percentage > MAX_BUDGET_MILESTONE:
                raise forms.ValidationError("Milestone's budget must not exceed %s percentage" % MAX_BUDGET_MILESTONE)

            if form.estimated_delivery_date < current_control_date:
                raise forms.ValidationError("Milestones must be linear, check your delivery dates")

            # Set control variables for next iteration    
            current_control_date = form.estimated_delivery_date                
            total_percentage += form.budget_percentage

        if total_percentage != 100:
            raise forms.ValidationError("All milestones budget percentage should sum up to 100%")

当我提交包含3个空里程碑表单的表单时,它对第一个没有任何作用     forms.ValidationError(...)

我已经通过提升异常()进行了验证,实际上它进入了if,但它仍然没有发生任何事情。

这是我的代码中的错误还是错误理解的ValidationError概念?

非常感谢任何帮助,提前谢谢。

修改

我正在添加缺少的视图代码

class DelayedModelFormMixin(ModelFormMixin):
    def form_valid(self, form):
        self.object = form.save(commit=False)
        self.prepare_object_for_save(self.object)
        self.object.save()
        if hasattr(self.object, "save_m2m"):
            self.object.save_m2m()
        return super(ModelFormMixin, self).form_valid(form)

    def prepare_object_for_save(self, obj):
        pass

class ProjectNew(CreateView, DelayedModelFormMixin):
    model = Project
    success_url = '/projects/project/%(slug)s'
    form_class = ProjectForm

    @method_decorator(login_required)
    def dispatch(self, request, *args, **kwargs):
        return super(ProjectNew, self).dispatch(request, *args, **kwargs)

    def prepare_object_for_save(self, obj):
        obj.owner = self.request.user
        # Code for stacked milestones and rewards
        context = self.get_context_data()
        milestone_form = context['milestone_formset']
        reward_form = context['reward_formset']
        if milestone_form.is_valid() and reward_form.is_valid():
            self.object = form.save()
            milestone_form.instance = self.object
            milestone_form.save()
            reward_form.instance = self.object
            reward_form.save()
            return HttpResponseRedirect(success_url)

    def form_invalid(self, form):
        return self.render_to_response(self.get_context_data(form=form))

    def get_context_data(self, **kwargs):
        context = super(ProjectNew, self).get_context_data(**kwargs)
        if self.request.POST:
            context['milestone_formset'] = MilestoneFormSet(self.request.POST)
            context['reward_formset'] = RewardFormSet(self.request.POST)
        else:
            context['milestone_formset'] = MilestoneFormSet()
            context['reward_formset'] = RewardFormSet()
        return context

1 个答案:

答案 0 :(得分:0)

如果我理解你的问题,问题在于你在计算表单集中有多少表单,但是你想要/需要计算表单集中有多少BOUND表单。

所以,我认为你需要改变这一行:

if len(self.forms) < REQUIRED_MILESTONES:

这些行:

bounded_forms = filter(lambda form:form.isbound(), self.forms)
if len(bounded_forms) < REQUIRED_MILESTONES:

然后您可以在执行此操作时引发验证错误。

修改

也许您不了解formset验证的工作原理。可以this可以提供帮助,特别是这句话:

  

在调用所有Form.clean方法之后调用formset clean方法。可以使用formset上的non_form_errors()方法找到错误。

如果我明白你说的话是什么意思

  

当我提交包含3个空里程碑表单的表单时,它对第一个表单没有任何作用.ValidationError(...)

然后,您没有看到引发的错误。因此,查找它们的地方是formset“non_form_errors”方法。在你的模板中加入这样的东西:

<span>{{milestone_formset.non_form_errors}}</span>

现在你应该看到你在里程碑formset clean方法中出现的错误。

希望它有所帮助!