回到顶部

阅读目录

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`

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

本人期望

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

实现代码

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

^_^
请喝咖啡 ×

文章部分资料可能来源于网络,如有侵权请告知删除。谢谢!

前一篇: django2 The custom handler404 view 'users.views.page_not_found' does not take the correct number of arguments (request, exception).
下一篇: HINT: ForeignKey(unique=True) is usually better served by a OneToOneField.