【发布时间】:2020-08-02 03:21:29
【问题描述】:
我是一名刚接触编程的学生。我正在开发一个创建计时功能的项目。我希望能够显示用户在项目上工作的总时间。我已经能够显示一个用户的总时间和一个项目的总时间。
class User(models.Model):
class Project(models.Model):
title = models.CharField(max_length=255)
start_date = models.DateField()
end_date = models.DateField()
done = models.BooleanField(default=False)
created_by = models.ForeignKey(User, related_name ='made_by', on_delete=models.CASCADE)
projects_working_on = models.ManyToManyField(User, related_name = "projects_assigned_to")
class Timekeeper(models.Model):
clock_in = models.DateTimeField(null=True)
clock_out = models.DateTimeField(null=True)
total_time = models.DurationField(null=True, blank=True)
entire_time = models.FloatField(null=True)
is_working = models.BooleanField(default=False)
users_time = models.ForeignKey(User, related_name="time_of_user", on_delete=models.CASCADE)
proj_time = models.ForeignKey(Project, related_name = 'time_of_project', on_delete=models.CASCADE)
这是打卡功能:
def clockout(request, proj_id):
user = User.objects.get(id=request.session['userid'])
now = datetime.now(timezone.utc)
this_proj = Project.objects.get(id = proj_id)
this_time = user.time_of_user.last()
time = this_time.users_time
this_time.clock_out = now
this_time.is_working = False
newtime = user.time_of_user.filter(proj_time=proj_id).aggregate(Sum('total_time'))
# this_time.total_time_two = newtime
this_time.save()
Timekeeper.objects.update(total_time=F('clock_out') - F('clock_in'))
Timekeeper.objects.update(entire_time=F('total_time'))
Timekeeper.objects.update(total_time_two=newtime)
# Timekeeper.objects.update(entire_time=user.time_of_user.filter(proj_time=proj_id).aggregate(Sum"(F('total_time')")
return redirect('/dashboard/view/'+str(proj_id))
整个时间字段的存在仅用于另一个函数迭代并查找特定用户或项目的所有字段的时间。我似乎无法获得特定项目的 1 个用户的 total_times(或 whole_times)的总和。非常感谢任何帮助。
【问题讨论】: