我需要在一个表(周)上设置开始日期和结束日期,然后基于这些值,自动填充另一个表(天)以及这些日期之间的日期。
我的模特是:
class Week(models.Model):
class Meta:
constraints = [models.UniqueConstraint(fields=['sub_unit', 'initial_date', 'last_date'], name='unique_item')]
ordering = ['-initial_date']
verbose_name = 'Week'
verbose_name_plural = 'Weeks'
sub_unit = models.CharField(max_length=255, null=True, blank=True, verbose_name='SubUnit')
initial_date = models.DateField(blank=False, null=True, verbose_name='Initial Date')
last_date = models.DateField(blank=False, null=True, verbose_name='Last Date')
def __str__(self):
return '{} - Week from {} to {}'.format(self.sub_unit, self.initial_date, self.last_date)
class Day(models.Model):
class Meta:
constraints = [models.UniqueConstraint(fields=['week', 'day'], name='unique_item')]
ordering = ['-day']
verbose_name = 'Day'
verbose_name_plural = 'Days'
week = models.ForeignKey(Week, related_name='week', on_delete=models.CASCADE)
day = models.DateField(blank=False, null=True, verbose_name='day')
def __str__(self):
return '{}'.format(self.day)
有人可以使我朝正确的方向前进吗?谢谢!