验证 Django 模型表单中的 end_date 大于 start_date

发布于 2024-12-03 13:19:56 字数 97 浏览 0 评论 0原文

我的模型中有一个 start_date 和 end_date 字段,当 end_date 大于 start_date 时,我想将错误分配给它,我一直在寻找文档,但没有找到相关示例。

I have a start_date and end_date fields in my model, I want to assign an error to end_date when it is bigger than start_date, I have been looking docs, but don't find an example about that.

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

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

发布评论

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

评论(3

吃颗糖壮壮胆 2024-12-10 13:19:56

您的表单中需要一个自定义清理函数来执行检查:

def clean(self):
    cleaned_data = super().clean()
    start_date = cleaned_data.get("start_date")
    end_date = cleaned_data.get("end_date")
    if end_date < start_date:
        raise forms.ValidationError("End date should be greater than start date.")

You need a custom clean function in your form that does the check:

def clean(self):
    cleaned_data = super().clean()
    start_date = cleaned_data.get("start_date")
    end_date = cleaned_data.get("end_date")
    if end_date < start_date:
        raise forms.ValidationError("End date should be greater than start date.")
烂柯人 2024-12-10 13:19:56

这是 Django 2.2 的更新 - 文档

from django import forms
from .models import Project

class ProjectAddForm(forms.ModelForm):
    class Meta:
        model = Project
        fields = [
            'name', 
            'overview',
            'start_date',
            'end_date',
            'status',
            'completed_on',
        ]

        labels = {
            "name": "Project Name",
            "overview": "Project Overview",
            "status": "Project Status",
        }

    # Logic for raising error if end_date < start_date
    def clean(self):
        cleaned_data = super().clean()
        start_date = cleaned_data.get("start_date")
        end_date = cleaned_data.get("end_date")
        if end_date < start_date:
            raise forms.ValidationError("End date should be greater than start date.")

This is an update for Django 2.2 - doc

from django import forms
from .models import Project

class ProjectAddForm(forms.ModelForm):
    class Meta:
        model = Project
        fields = [
            'name', 
            'overview',
            'start_date',
            'end_date',
            'status',
            'completed_on',
        ]

        labels = {
            "name": "Project Name",
            "overview": "Project Overview",
            "status": "Project Status",
        }

    # Logic for raising error if end_date < start_date
    def clean(self):
        cleaned_data = super().clean()
        start_date = cleaned_data.get("start_date")
        end_date = cleaned_data.get("end_date")
        if end_date < start_date:
            raise forms.ValidationError("End date should be greater than start date.")
凉风有信 2024-12-10 13:19:56

这是 文档中实际推荐的示例

简而言之,记住返回cleaned_data,并正确引发表单错误。

from django import forms

class ContactForm(forms.Form):
    # Everything as before.
    ...

    def clean_recipients(self):
        data = self.cleaned_data['recipients']
        if "[email protected]" not in data:
            raise forms.ValidationError("You have forgotten about Fred!")

    # Always return the cleaned data, whether you have changed it or
    # not.
    return data

This is the actual recommended example from the docs

In short, remember to return cleaned_data, and raise form errors correctly.

from django import forms

class ContactForm(forms.Form):
    # Everything as before.
    ...

    def clean_recipients(self):
        data = self.cleaned_data['recipients']
        if "[email protected]" not in data:
            raise forms.ValidationError("You have forgotten about Fred!")

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