Django 自定义(多)小部件输入验证

发布于 2024-10-12 15:04:07 字数 1108 浏览 3 评论 0原文

在以下每种情况下验证自定义多小部件输入的正确方法是什么:

  1. 如果我想实现自定义字段?
  2. 如果我想使用现有的数据库字段类型(例如 DateField)?

这样做的动机来自以下两个问题:

  1. 我如何使用 django多部件
  2. Django 子类化 multiwidget

我特别感兴趣事实上我觉得我被骗了。我已经使用了 value_from_datadict() ,如下所示:

def value_from_datadict(self, data, files, name):

    datelist = [widget.value_from_datadict(data, files, name + '_%s' % i) for i, widget in enumerate(self.widgets)]
    try:   
        D = date(day=int(datelist[0]), month=int(datelist[1]), year=int(datelist[2]))
        return str(D)
    except ValueError:
        return None

它查看 POST 字典并为我的小部件构造一个值(请参阅链接的问题)。然而,与此同时我还进行了一些验证;也就是说,如果创建 D 作为日期对象失败,我将返回 None ,这将在 is_valid() 检查中失败。

因此,我的第三个问题是我应该以其他方式这样做吗?对于这种情况,我不需要自定义字段。

谢谢。

What is the correct method for validating input for a custom multiwidget in each of these cases:

  1. if I want to implement a custom Field?
  2. if I want to use an existing database field type (say DateField)?

The motivation for this comes from the following two questions:

  1. How do I use django's multi-widget?
  2. Django subclassing multiwidget

I am specifically interested in the fact that I feel I have cheated. I have used value_from_datadict() like so:

def value_from_datadict(self, data, files, name):

    datelist = [widget.value_from_datadict(data, files, name + '_%s' % i) for i, widget in enumerate(self.widgets)]
    try:   
        D = date(day=int(datelist[0]), month=int(datelist[1]), year=int(datelist[2]))
        return str(D)
    except ValueError:
        return None

Which looks through the POST dictionary and constructs a value for my widget (see linked questions). However, at the same time I've tacked on some validation; namely if the creation of D as a date object fails, I'm returning None which will fail in the is_valid() check.

My third question therefore is should I be doing this some other way? For this case, I do not want a custom field.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

几味少女 2024-10-19 15:04:07

您可以像验证任何其他字段一样验证表单字段,并在表单中实现 clean_fieldname 方法。如果您的验证逻辑分布在许多表单字段(这与许多小部件相同!),您可以将其放入表单的 clean() 方法中。

http://docs.djangoproject.com/en/1.2/ref/forms/验证/

You validate your form fields just like any other fields, implementing the clean_fieldname method in your form. If your validation logic spreads across many form fields (which is nto the same as many widgets!) you put it in your form's clean() method.

http://docs.djangoproject.com/en/1.2/ref/forms/validation/

陈年往事 2024-10-19 15:04:07

根据文档,验证是小部件背后的字段的责任,而不是小部件本身。小部件除了向用户呈现输入并将输入数据传递回字段之外,什么也不做。

因此,如果您想验证已提交的数据,您应该 编写验证器

这对于 MultiWidget 来说尤其重要,因为您可能会出现多个方面的数据错误。每个方面都需要返回给用户以供考虑,内置的方法是编写验证器并将它们放置在字段的 validators 属性中。

与文档相反,您不必针对每个表单执行此操作。相反,您可以扩展内置表单之一并向 default_validators 添加一个条目。

还有一点需要注意:如果您要实现 MultiWidget,您的表单会将某种“压缩”数据传递回它进行渲染。文档说:

此方法从字段中获取单个“压缩”值并返回“解压缩”值列表。可以假定输入值有效,但不一定非空。

-小部件< /a>

只要确保您正确处理该输出就可以了。

According to the documentation, validation is the responsibility of the field behind the widget, not the widget itself. Widgets should do nothing but present the input for the user and pass input data back to the field.

So, if you want to validate data that's been submitted, you should write a validator.

This is especially important with MultiWidgets, as you can have more than one aspect of the data error out. Each aspect needs to be returned to the user for consideration, and the built in way to do that is to write validators and place them in the validators attribute of the field.

Contrary to the documentation, you don't have to do this per form. You can, instead, extend one of the built in forms and add an entry to default_validators.

One more note: If you're going to implement a MultiWidget, your form is going to pass some sort of 'compressed' data back to it to render. The docs say:

This method takes a single “compressed” value from the field and returns a list of “decompressed” values. The input value can be assumed valid, but not necessarily non-empty.

-Widgets

Just make sure you're handling that output correctly and you'll be fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文