简体   繁体   English

sysdate依赖日期验证程序引起的合成/冗余Django迁移

[英]Synthetic/redundant Django migrations caused by sysdate dependent date validator

I have a date field like the one below in a Django model, where I want the date to be in the future (or today, but not in the past). 我在Django模型中有一个类似于下面的日期字段,我希望该日期是将来的日期(或今天,而不是过去的日期)。

foo = models.DateField('Foo', null=True,
        validators=[MinValueValidator(date.today())])

This works fine, validation happens as expected on forms, however, makemigrations creates a new migration every day it runs with that date, below is an example produced today: 这可以正常工作,可以按预期在表单上进行验证,但是, makemigrations在该日期运行的每一天makemigrations创建一个新的迁移,以下是今天产生的示例:

field=models.DateField(null=True,
    validators=[django.core.validators.MinValueValidator(
        datetime.date(2018, 1, 26))]

How can I avoid this? 如何避免这种情况? Should I use a custom validator instead of MinValueValidator or add a clean method to the Form subclass instead like this ? 我应该使用一个自定义的验证,而不是MinValueValidator或添加一个clean方法将Form的子类,而不是像这样

There's no way to avoid this. 无法避免这种情况。 You've got a couple of options. 您有两种选择。

  • Write a custom validator. 编写一个自定义验证器。
  • Write a clean_fieldname method in your forms. 在您的表单中编写一个clean_fieldname方法。
  • Write a pre_save signal on the model which makes sure the value is correct. 在模型上写入一个pre_save信号,以确保该值正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM