Django Formsets - form.is_valid()为False,阻止了formset验证

时间:2009-11-05 17:03:50

标签: python django django-forms

我正在使用formset来让用户订阅多个Feed。我要求a)用户通过选择布尔字段来选择订阅,并且还需要标记订阅和b)用户必须订阅指定数量的订阅。

目前,下面的代码能够a)确保用户标记订阅,但是我的一些表单is_valid()是False,因此阻止了我对完整formset的验证。 [编辑]此外,相关的formset错误消息无法显示。

以下是代码:

from django import forms
from django.forms.formsets import BaseFormSet
from tagging.forms import TagField
from rss.feeder.models import Feed 


class FeedForm(forms.Form):
    subscribe = forms.BooleanField(required=False, initial=False)
    tags = TagField(required=False, initial='')

    def __init__(self, *args, **kwargs):
        feed = kwargs.pop("feed")
        super(FeedForm, self).__init__(*args, **kwargs)
        self.title = feed.title
        self.description = feed.description

    def clean(self):
        """apply our custom validation rules"""
        data = self.cleaned_data
        feed = data.get("subscribe")
        tags = data.get("tags")
        tag_len = len(tags.split())
        self._errors = {}
        if feed == True and tag_len < 1:
            raise forms.ValidationError("No tags specified for feed")
        return data



class FeedFormSet(BaseFormSet):

    def __init__(self, *args, **kwargs):
        self.feeds = list(kwargs.pop("feeds"))
        self.req_subs = 3    # TODO: convert to kwargs arguement
        self.extra = len(self.feeds)
        super(FeedFormSet, self).__init__(*args, **kwargs)

    # WARNING! Using  undocumented. see   for details...
    def _construct_form(self, i, **kwargs):
        kwargs["feed"] = self.feeds[i]
        return super(FeedFormSet, self)._construct_form(i, **kwargs)


    def clean(self):
        """Checks that only a required number of Feed subscriptions are present"""
        if any(self.errors):
            # Do nothing, don't bother doing anything unless all the FeedForms are valid
            return
        total_subs = 0
        for i in range(0, self.extra):
            form = self.forms[i]
            feed = form.cleaned_data
            subs = feed.get("subscribe")
            if subs == True:
                total_subs += 1
        if total_subs != self.req_subs:
            raise forms.ValidationError("More subscriptions...") # TODO more informative
        return form.cleaned_data

根据要求,查看代码:

from django.forms import formsets
from django.http import Http404
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response

from rss.feeder.forms import FeedForm
from rss.feeder.forms import FeedFormSet
from rss.feeder.models import Feed

FeedSet = formsets.formset_factory(FeedForm, FeedFormSet)

def feeds(request):
    if request.method == "POST":
        formset = create_feed_formset(request.POST)
        if formset.is_valid():
            # submit the results
            return HttpResponseRedirect('/feeder/thanks/')
    else:
        formset = create_feed_formset() 
    return render_to_response('feeder/register_step_two.html', {'formset': formset})    


def create_feed_formset(data=None):
    """Create and populate a feed formset"""
    feeds = Feed.objects.order_by('id')
    if not feeds:
        # No feeds found, we should have created them
        raise Http404('Invalid Step')
    return FeedSet(data, feeds=feeds)        # return the instance of the formset

任何帮助都将不胜感激。

聚苯乙烯。如需完整披露,此代码基于http://google.com/search?q=cache:rVtlfQ3QAjwJ:https://www.pointy-stick.com/blog/2009/01/23/advanced-formset-usage-django/+django+formset

[已解决]见下面的解决方案。

2 个答案:

答案 0 :(得分:3)

解决。以下是该解决方案的快速浏览。

报告错误,需要操作和格式化特殊错误消息。在formsets的源代码中,我发现适用于整个表单的错误称为 non_form_errors ,并基于此产生自定义错误。 [注意:我找不到任何关于此的权威文档,所以有人可能知道更好的方法]。代码如下:

def append_non_form_error(self, message):
    errors = super(FeedFormSet, self).non_form_errors()
    errors.append(message)
    raise forms.ValidationError(errors)

formset clean方法也需要一些调整。基本上它会检查表单是否绑定(空的表单不是,因此问题中is_valid为false),如果是,则访问检查会有订阅值。

def clean(self):
    """Checks that only a required number of Feed subscriptions are present"""
    count = 0
    for form in self.forms:
        if form.is_bound:
            if form['subscribe'].data:
                count += 1
    if count > 0 and count != self.required:
        self.append_non_form_error("not enough subs")

有些人可能想知道为什么我选择使用 form ['field_name']。data 格式来访问该值。这允许我们检索原始值并始终获得订阅的计数,允许我返回整个formset的所有相关消息,即单个表单的特定问题和更高级别的问题(如订阅数量),这意味着用户赢了不得不一遍又一遍地重新提交表单以完成错误列表。

最后,我错过了我的模板的一个重要方面, {{formset.non_form_errors}} 标记。以下是更新后的模板:

{% extends "base.html" %}
{% load i18n %}

{% block content %}
<form action="." method="post">
 {{ formset.management_form }}
 {{ formset.non_form_errors }}
    <ol> 
        {% for form in formset.forms %}
        <li><p>{{ form.title }}</p>
   <p>{{ form.description }}</p>
        {{ form.as_p }}
        </li>
        {% endfor %}
    </ol>
    <input type="submit">
</form>

{% endblock %}

答案 1 :(得分:0)

我试图绕过我的问题......它不是一个好的解决方案,它非常像是一个黑客。它允许人们在订阅所需数量的订阅源时(在低于1的情况下)继续进行,但如果少于所需的订阅源数量,则无法显示引发的错误消息。

def clean(self):
    count = 0
    for i in range(0, self.extra):
        form = self.forms[i]
        try:
            if form.cleaned_data:
                count += 1
        except AttributeError:
            pass
    if count > 1:
        raise forms.ValidationError('not enough subscriptions')
    return form.cleaned_data

我在我的模板中使用{{formset.management_form}},因为据我所知,错误应该显示。在我的模板下面以防我被误导。

{% extends "base.html" %}
{% load i18n %}

{% block content %}
<form action="." method="post">
    {{ formset.management_form }}
    <ol> 
        {% for form in formset.forms %}
        {{ form.as_p }}
        </li>
        {% endfor %}
    </ol>
    <input type="submit">
</form>

{% endblock %}