Django:如何根据另一个注释进行注释,或对注释执行操作
所以我正在制作一些可以组织学校日程的东西。该学校的组织方式是,班级一起呆在同一个房间,老师会移动,每天的时间表都不同,而且并非所有老师或班级都在同一时间在大楼里,有些的老师在教学时非常挑剔。
我有这些模型:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无法使用 annotate 方法计算这样的比率。您需要使用 extra() 提供的方法查询集 API。虽然不建议使用
extra
进行查找,因为它们不可跨数据库引擎移植,但有时它们是执行此类复杂查询的最佳(或唯一)选项。假设您引用的所有模型都位于名为
schedules
的应用中,并且您尚未使用模型元类中的db_table
属性修改表名称,查找将是这样的: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 thedb_table
attribute in your models' metaclasses, the lookup would be something like this: