Django 自定义字段验证器与 clean

发布于 2024-11-16 01:10:47 字数 232 浏览 2 评论 0原文

我想创建 TodayOrLaterDateField() 它将子类化 DateField() 字段,因为我在很多地方使用这种条件。该字段的目的是避免输入过去的日期。

做到这一点最直接的方法是什么?我对验证器与干净方法感到困惑。 我尝试过使用 clean() ,但是当将值与 datetime.date.today() 进行比较时,我收到“将 unicode 对象与日期进行比较”错误。

我正在使用 Django 1.3

I would like to create TodayOrLaterDateField() which would subclass DateField() field as I am using this condition in many places. The purpose of this field would be avoiding putting dates from the past.

What is the most straightway way of doing this? I am confused with validator vs. clean method.
I've tried with clean() but when comparing value to datetime.date.today() I am getting "compare unicode object to date" error.

I'm using Django 1.3

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

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

发布评论

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

评论(2

这个俗人 2024-11-23 01:10:47

验证器仅进行验证,不返回改进后的格式;
干净的方法既验证又返回一个(有时是修改的)值。

我认为这里的方法是仅使用带有验证器的 DateField 作为具有 default_validators 设置的 DateField 的继承类。

import datetime
from django.core import exceptions
from django.db import models
from django.utils.translation import ugettext_lazy as _

def validate_date_today_or_later(value):
    'Place this in validators.py and import it to keep your model a bit cleaner'
    if value < datetime.date.today():
        raise exceptions.ValidationError(_('Date must be today or later'))

class TodayOrLaterDateField(models.DateField):
    default_validators = [validate_date_today_or_later,]

编辑:
如果您只想将相同的验证器应用到表单字段而不是整个应用程序中,则也可以将其应用到表单字段。

Validators only validate, they don't return the improved format;
Clean methods both validate and return a (sometimes amended) value.

I think the way to go here is to just use a DateField with a validator as a inherited class of DateField with a default_validators set.

import datetime
from django.core import exceptions
from django.db import models
from django.utils.translation import ugettext_lazy as _

def validate_date_today_or_later(value):
    'Place this in validators.py and import it to keep your model a bit cleaner'
    if value < datetime.date.today():
        raise exceptions.ValidationError(_('Date must be today or later'))

class TodayOrLaterDateField(models.DateField):
    default_validators = [validate_date_today_or_later,]

edit:
You can apply the same validator to your form fields as well if you just want it there and not in your whole app.

方觉久 2024-11-23 01:10:47

您可以扩展 models.DateField 并重写 to_python 方法。没有在 Django 1.3 上进行测试,但应该可以工作。

import datetime
from django.core import exceptions
from django.db import models

class TodayOrLaterDateField(models.DateField):
    def to_python(self, value):
        value = super(TodayOrLaterDateField, self).to_python(value)
        if value < datetime.date.today():
            raise exceptions.ValidationError(u'Date must be today or later')
        return value

You can extend models.DateField and override to_python method. Didn't tested on Django 1.3 but should work.

import datetime
from django.core import exceptions
from django.db import models

class TodayOrLaterDateField(models.DateField):
    def to_python(self, value):
        value = super(TodayOrLaterDateField, self).to_python(value)
        if value < datetime.date.today():
            raise exceptions.ValidationError(u'Date must be today or later')
        return value
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文