Django:如何根据另一个注释进行注释,或对注释执行操作

发布于 2024-12-17 09:36:08 字数 1133 浏览 3 评论 0原文

所以我正在制作一些可以组织学校日程的东西。该学校的组织方式是,班级一起呆在同一个房间,老师会移动,每天的时间表都不同,而且并非所有老师或班级都在同一时间在大楼里,有些的老师在教学时非常挑剔。

我有这些模型:

class Teacher(models.Model):
    christian_name = models.CharField(max_length=200)
    family_name = models.CharField(max_length=200)
    ...

class TeacherAvailableHour(models.Model):
    teacher = models.ForeignKey('Teacher')

class Requirement(models.Model):
    ...
    teacher = models.ForeignKey('Teacher')
    per_week = models.PositiveIntegerField()
    ...

所以我想做的事情是首先为最挑剔的老师制定一个时间表,即他们可以教学的时间(总“TeacherAvailableHour”)和他们需要的小时数之间比例最小的老师教学(教师所有“Requirement.per_week”的总和)。

换句话说,我需要“order_by”该比率。我想不出该怎么做。尝试过这个:

Teachers=Teacher.objects.annotate(availability=(Count('teacheravailablehour') / Sum('requirement__per_week')).order_by('availability')

当然,这会因错误而爆炸。我还尝试过类似双重注释的方法:

Teachers=Teacher.objects.annotate(availability=Count('teacheravailablehour')).annotate(required=Sum('requirement__per_week')).annotate(availRatio=(('availability') / ('required')).order_by('-availRatio')

执行此操作的最佳方法是什么?这可能吗?

So I am making something that organizes a school schedule. The school in question is organized in a way that the classes stay together in the same room and the teacher moves, the schedule is different every day, and not all the teachers or the classes are in the building in at the same time, and some of the teachers are very picky about when they teach.

I have these models:

class Teacher(models.Model):
    christian_name = models.CharField(max_length=200)
    family_name = models.CharField(max_length=200)
    ...

class TeacherAvailableHour(models.Model):
    teacher = models.ForeignKey('Teacher')

class Requirement(models.Model):
    ...
    teacher = models.ForeignKey('Teacher')
    per_week = models.PositiveIntegerField()
    ...

So the thing I am trying to do is make a schedule for the pickiest teachers first, namely the ones with the smallest ratio between when they can teach (total "TeacherAvailableHour"s) and the number of hours they are required to teach (the sum of all of the teacher's "Requirement.per_week").

In other words, I need to "order_by" that ratio. I can't think of how to do it. Tried this:

Teachers=Teacher.objects.annotate(availability=(Count('teacheravailablehour') / Sum('requirement__per_week')).order_by('availability')

That blows up with an error, of course. I've also tried something like a double annotation:

Teachers=Teacher.objects.annotate(availability=Count('teacheravailablehour')).annotate(required=Sum('requirement__per_week')).annotate(availRatio=(('availability') / ('required')).order_by('-availRatio')

What is the best way to do this? Is this even possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

君勿笑 2024-12-24 09:36:08

您无法使用 annotate 方法计算这样的比率。您需要使用 extra() 提供的方法查询集 API。虽然不建议使用 extra 进行查找,因为它们不可跨数据库引擎移植,但有时它们是执行此类复杂查询的最佳(或唯一)选项。

假设您引用的所有模型都位于名为 schedules 的应用中,并且您尚未使用模型元类中的 db_table 属性修改表名称,查找将是这样的:

availability_sql = "(select count(*) from schedules_teacheravailablehour where schedules_teacheravailablehour.teacher_id=schedules_teacher.id) * 1.0"
required_hrs_sql = "(select sum(per_week) from schedules_requirement where schedules_requirement.teacher_id=schedules_teacher.id)"
Teacher.objects.extra(select={"ratio":"%s/%s" %(availability_sql, required_hrs_sql)}).order_by("-ratio")

You cannot calculate such a ratio using the annotate method. You need to use the extra() method provided by the QuerySet API. While lookups using extra are not advisable as they are not portable across DB engines, they are sometimes the best (or only) option for doing such complex queries.

Assuming all the models you are referencing are in an app named schedules, and you have not modified table names using the db_table attribute in your models' metaclasses, the lookup would be something like this:

availability_sql = "(select count(*) from schedules_teacheravailablehour where schedules_teacheravailablehour.teacher_id=schedules_teacher.id) * 1.0"
required_hrs_sql = "(select sum(per_week) from schedules_requirement where schedules_requirement.teacher_id=schedules_teacher.id)"
Teacher.objects.extra(select={"ratio":"%s/%s" %(availability_sql, required_hrs_sql)}).order_by("-ratio")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文