【问题标题】:set the Django's model to a specific day in a month?将 Django 的模型设置为一个月中的特定日期?
【发布时间】:2014-12-20 15:13:36
【问题描述】:

我有一个月度报告模型,我想将其设置为特定日期(周期)。 这是我的模型:

class MonthlyReport(m.Model):
    month  = m.DateField()  <=   set to a specific day in a month
    worker = m.ForeignKey('auth.User')
    total  = m.IntegerField()
    cycle  = m.DateField(blank=True, null=True)
    approvedDate = m.DateTimeField(blank=True, null=True)

【问题讨论】:

  • 您是要求设置默认值还是在创建新对象时这样做?

标签: django model datefield


【解决方案1】:

您可以设置auto_now=True,以便在对象更新后设置为实际日期:

month  = m.DateField(auto_now=True) 

您的模型只是报表类的定义。在特定月份创建/更新报告对象后,特定月份将设置为其对象。

may_report = MonthlyReport(worker=some_user, total=23, .....)
may_report.save()

month 字段会自动更新为实际月份的日期。你不需要为它做任何事情。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-06
    • 1970-01-01
    • 1970-01-01
    • 2015-03-06
    • 1970-01-01
    • 2015-10-10
    • 2019-05-14
    • 2018-05-10
    相关资源
    最近更新 更多