我如何在 django 中比较两个模型字段?

发布于 2024-10-19 01:18:01 字数 305 浏览 1 评论 0原文

我的 Models.py 文件中有这个模型。我想比较“start_date”和“end_date”,以便 start_date 值永远不会大于 end_date,反之亦然。我如何进行此验证?

class Completion(models.Model):

    start_date = models.DateField()
    end_date = models.DateField()
    batch = models.ForeignKey(Batch)
    topic = models.ForeignKey(Topic)

I have this Model in my Models.py file.I want to to compare "start_date" and "end_date" so that start_date value would never be greater then end_date or vice-versa.How do i do this validation?

class Completion(models.Model):

    start_date = models.DateField()
    end_date = models.DateField()
    batch = models.ForeignKey(Batch)
    topic = models.ForeignKey(Topic)

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

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

发布评论

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

评论(1

邮友 2024-10-26 01:18:01

我将开始让您了解模型验证框架。
http:https://docs .djangoproject.com/en/2.0/ref/models/instances/#django.db.models.Model.clean

它由 ModelForms 使用,并且开始很有意义使用它。

基本上,您可以在模型中定义一个 clean() 方法,放入验证逻辑,并在失败时引发 ValidationError

class MyModel(models.Model):
    def clean(self):
        from django.core.exceptions import ValidationError
        if self.start_data > self.end_date:
            raise ValidationError('Start date cannot precede end date')


    def save(self, *args, **kwargs):
        # you can have regular model instance saves use this as well
        super(MyModel, self).save(*args, **kwargs)

这里的好处是任何ModelForm(这意味着管理站点也会调用full_clean(),这反过来会调用您的模型clean() 无需任何额外工作。

无需覆盖 save_model,您将在管理表单顶部看到常见的验证错误。
description

最后,它非常方便。您可以在任何地方使用它。

try:
    my_model.full_clean()
except ValidationError, e:
    # Do something based on the errors contained in e.message_dict.
    # Display them to a user, or handle them programatically.

I'd start getting your head into the Model Validation framework.
http:https://docs.djangoproject.com/en/2.0/ref/models/instances/#django.db.models.Model.clean

It's used by ModelForms and just makes a whole lot of sense to start using it.

Basically, you'd define a clean() method in your model, put in your validation logic, and raise ValidationError if it fails.

class MyModel(models.Model):
    def clean(self):
        from django.core.exceptions import ValidationError
        if self.start_data > self.end_date:
            raise ValidationError('Start date cannot precede end date')


    def save(self, *args, **kwargs):
        # you can have regular model instance saves use this as well
        super(MyModel, self).save(*args, **kwargs)

The benefit here is that any ModelForm (which means the admin site too will call full_clean(), which in turn calls your model clean() without any extra work.

No need to override save_model, you'll get the usual validation errors at the top of the admin form.
description

Finally, it's super convenient. You can use it anywhere.

try:
    my_model.full_clean()
except ValidationError, e:
    # Do something based on the errors contained in e.message_dict.
    # Display them to a user, or handle them programatically.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文