set the Django's model to a specific day in a month?

537 Views Asked by At

I have a monthly report model and I want to set it to a specific date (cycle). This is my model:

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)
1

There are 1 best solutions below

0
On

you could set auto_now=True so it is set to actual date once object is updated:

month  = m.DateField(auto_now=True) 

your model is just a definition of a report class. the specific month will be set to its object once the report object is created/updated in that specific month.

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

month field is automatically updated with actual month's date. you dont need to do anything for it.