Django Filter和Sum Value取决于不同的模型



_大家好!我试图将每个月(任务)的用户次数相加,如下所示:

times = goal.time_set.filter(day__year=today.year, day__month=today.month)

然后求和:

for time in times:
total_min[member_number] = total_min[member_number] + time.value

但是它计算当前月份(任务)。我要计算时间取决于对象模型的使命。模型的使命:

class Mission(models.Model):
name = models.CharField(max_length=200, default='')
description = models.TextField(default='')
add_info = models.TextField(default='', blank=True)
theme = models.ImageField(upload_to='themes/', default='themes/default.png')
date_created = models.DateTimeField(null=True)
date_finished = models.DateTimeField(null=True, blank=True)

我是这样想的:

times = goal.time_set.filter(day__year=mission.date_created.year, day__month=mission.month_created.month)

我怎样才能实现它?

乌利希期刊指南。目标模型:

class Goal(models.Model):
STATUS = (
('in progress', 'in progress'),
('completed', 'completed'),
('failed', 'failed')
)
id_ninja = models.ForeignKey(Ninja, null=True, on_delete=models.SET_NULL)
id_user = models.ForeignKey(User, null=True, on_delete=models.SET_NULL)
name = models.CharField(max_length=150, default='')
description = models.TextField(default='')
date_created = models.DateTimeField(null=True)
status = models.CharField(max_length=20, choices=STATUS, default='in progress')

我用我的方法解决了这个问题。

mission_year = mission.date_created.strftime('%Y')
mission_month = mission.date_created.strftime('%m')
times = goal.time_set.filter(day__year__gte=mission_year, day__month__gte=mission_month)

相关内容

  • 没有找到相关文章