在Django中使用自定义MultiValueWidget验证MultiValueField

时间:2011-12-05 13:34:01

标签: django django-forms django-multiwidget

我希望有一个字段和一个小工具,让我们有一个“近似”的日期,这个想法只有以下一个:

  • 确切日期
  • 一年,也许一个月
  • 初始年和结束年

一些压缩有效值可能是:

  • “1988年8月7日,,,,”
  • “1988 ,,,”
  • “1988,08 ,,”
  • ” ,,, 1997,1999"

问题是我无法验证字段只有上面描述的三个选项中的一个,在Field定义中我尝试创建验证和清理方法,但它没有得到验证,我怀疑它之间的链接字段和小部件不正确。我错过了什么?

为此,我创建了一个小部件:

class ApproximatedDateWidget(forms.MultiWidget):
    def __init__(self, attrs=None,choices=()):
        self.widgets = (
            forms.DateInput(attrs=attrs),
            forms.TextInput(attrs=attrs),
            forms.Select(attrs=attrs,choices=choices),
            forms.TextInput(attrs=attrs),
            forms.TextInput(attrs=attrs),
        )
        super(ApproximatedDateWidget, self).__init__(self.widgets, attrs=attrs)

    def decompress(self,value):
        try :
            print value
            if value:
                l=value.split(",")
                print l
                if l[0] and l[0]!="":                    
                    d=l[0].split("/")
                    print d
                    return [date(int(d[0]),int(d[1]),int(d[2])),None,None,None,None]
                else:
                    return [None, l[1], l[2], l[3], l[4]]
            return [None, None, None, None, None]
        except:
            raise
            return [None, None, None, None, None]

    def format_output(self,rendered_widgets):
        return mark_safe(u"""<table>
        <tr><td>Exact date:</td><td colspan="3">{0}</td></tr>
        <tr><td>Year and month:</td><td>{1}</td><td colspan="2">{2}</td></tr>
        <tr><td></td><td>Year</td><td colspan="2">Month</td></tr>
        <tr><td>Between:</td><td>{3}</td><td>and</td><td>{4}</td></tr>
        <tr><td></td><td>Year</td><td></td><td>Year<td></tr>
        </table>
        """.format(*rendered_widgets))

表格字段:

from tools import MonthChoices
from validators import nullableinteger

class ApproximatedDateField(forms.MultiValueField):
    """
    Field that allows having an approximated date, one of
    exact date, month and year or range of years
    """


    def __init__(self, required=False, widget=None, label=None, initial=None,
                 help_text=None,attrs={}, *args, **kwargs):
        self.widget = ApproximatedDateWidget(attrs=attrs,choices=MonthChoices)
        widgets=self.widget.widgets
        fields = (
            forms.DateField(required=required,validators=[lessthantomorrow],widget=widgets[0]),
            forms.CharField(widget=widgets[1],required=required,max_length=4,validators=[nullableinteger]),
            forms.ChoiceField(required=required,choices=MonthChoices,widget=widgets[2]),
            forms.CharField(widget=widgets[3],required=required,max_length=4,validators=[nullableinteger]),
            forms.CharField(widget=widgets[4],required=required,max_length=4,validators=[nullableinteger]),
        )
        super(ApproximatedDateField, self).__init__(fields=fields,required=required, widget=widget, 
              label=label, initial=initial, help_text=help_text)

    def compress(self,data_list):
        return """{0},{1},{2},{3},{4}""".format(data_list[0].strftime("%d/%m/%Y"),data_list[1],(data_list[2] and data_list[2].id) or "",data_list[3],data_list[4])

    def validate(self,values):
        super(ApproximatedDateField, self).validate(values)
        if values[0] is not None and (values[1] is not None or values[2] is not None) and (values[1] is not None or values[2] is not None):
            raise ValidationError(u'Please only fill exact date or year and month or the range years')

    def clean(self,values):
        super(ApproximatedDateField, self).clean(values)
        if values[0] is not None and (values[1] is not None or values[2] is not None) and (values[1] is not None or values[2] is not None):
            raise ValidationError(u'Please only fill exact date or year and month or the range years')

1 个答案:

答案 0 :(得分:1)

我编写了一个自定义小部件和多值字段来处理英尺和英寸:http://djangosnippets.org/snippets/2327/,还有一个是数学验证码:https://github.com/btaylordesign/django-simple-math-captcha

我将我的验证放在MultiValueField的compress方法中,这对于这些方法非常有用。您可以查看该片段和回购,看看是否能让您走上正确的轨道。