HINT: It seems you set a fixed date / time / datetime value as default for this field. This may not

2023-02-18 14:19:42 浏览数 (1)

WARNINGS

operations.AnchorBindAgent.start_time: (fields.W161) Fixed default value provided.     HINT: It seems you set a fixed date / time / datetime value as default for this field. This may not be what you want. If you want to have the current date as default, use `django.utils.timezone.now`

警告的意思

希望你 时间字段类型的默认值使用 timezone.now

本人期望

添加时间字段类型的时候,默认值自动延后多少天。

实现代码

代码语言:javascript复制
from django.utils import timezone
from datetime import timedelta


class AnchorBindAgent(models.Model):
    """
    主播绑定到代理,分成规则
    """
    end_time_default = (timezone.now()   timedelta(days=15)).date()

    agent = models.ForeignKey(verbose_name="代理", to=User, on_delete=models.DO_NOTHING)
    anchor = models.OneToOneField(verbose_name="主播", to=Anchor, on_delete=models.DO_NOTHING)
    proportion = models.FloatField(verbose_name="分成比例", default=10, help_text="0 到 100 整数或小数")

    start_time = models.DateField(verbose_name="生效日期", default=timezone.now)
    end_time = models.DateField(verbose_name="失效日期", default=end_time_default)

    create_time = models.DateTimeField(verbose_name="创建时间", auto_now_add=True)
    update_time = models.DateTimeField(verbose_name="更新时间", auto_now=True)

    def __str__(self):
        return "%s_%s_%s" % (self.agent, self.anchor, self.proportion)

    class Meta:
        verbose_name = "主播绑定"
        verbose_name_plural = "主播绑定列表"

Django 环境

2.2

0 人点赞