Python Django过滤器仅适用于聚合/注释
我正在尝试构造一个相当复杂的Django查询,但进展不大。我希望这里有个巫师能帮我 我有以下型号:Python Django过滤器仅适用于聚合/注释,python,django,database,django-models,django-queryset,Python,Django,Database,Django Models,Django Queryset,我正在尝试构造一个相当复杂的Django查询,但进展不大。我希望这里有个巫师能帮我 我有以下型号: class Person(models.Model): MALE = "M" FEMALE = "F" OTHER = "O" UNKNOWN = "U" GENDER_CHOICES = ( (MALE, "Male"), (FEMALE, "Female"), (UNKNOWN, "
class Person(models.Model):
MALE = "M"
FEMALE = "F"
OTHER = "O"
UNKNOWN = "U"
GENDER_CHOICES = (
(MALE, "Male"),
(FEMALE, "Female"),
(UNKNOWN, "Other"),
)
firstName = models.CharField(max_length=200, null=True, db_column="firstname")
lastName = models.CharField(max_length=200, null=True, db_column="lastname")
gender = models.CharField(max_length=1, choices=GENDER_CHOICES, default=UNKNOWN, null=True)
dateOfBirth = models.DateField(null=True, db_column="dateofbirth")
dateInService = models.DateField(null=True, db_column="dateinservice")
photo = models.ImageField(upload_to='person_photos', null=True)
class SuccessionTerm(models.Model):
originalName = models.CharField(max_length=200, null=True, db_column="originalname")
description = models.CharField(max_length=200, blank=True, null=True)
score = models.IntegerField()
class Succession(model.Model):
position = models.ForeignKey(Position, to_field='positionId', db_column="position_id")
employee = models.ForeignKey(Employee, to_field='employeeId', db_column="employee_id")
term = models.ForeignKey(SuccessionTerm)
class Position(models.Model):
positionId = models.CharField(max_length=200, unique=True, db_column="positionid")
title = models.CharField(max_length=200, null=True)
# There cannot be a DB constraint, as that would make it impossible to add the first position.
dottedLine = models.ForeignKey("Position", to_field='positionId', related_name="Dotted Line",
null=True, db_constraint=False, db_column="dottedline_id")
solidLine = models.ForeignKey("Position", to_field='positionId', related_name="SolidLine",
null=True, db_constraint=False, db_column="solidline_id")
grade = models.ForeignKey(Grade)
businessUnit = models.ForeignKey(BusinessUnit, null=True, db_column="businessunit_id")
functionalArea = models.ForeignKey(FunctionalArea, db_column="functionalarea_id")
location = models.ForeignKey(Location, db_column="location_id")
class Employee(models.Model):
person = models.OneToOneField(Person, db_column="person_id")
fte = models.IntegerField(default=100)
dataSource = models.ForeignKey(DataSource, db_column="datasource_id")
talentStatus = models.ForeignKey(TalentStatus, db_column="talentstatus_id")
retentionRisk = models.ForeignKey(RetentionRisk, db_column="retentionrisk_id")
retentionRiskReason = models.ForeignKey(RetentionRiskReason, db_column="retentionriskreason_id")
performanceStatus = models.ForeignKey(PerformanceStatus, db_column="performancestatus_id")
potential = models.ForeignKey(Potential, db_column="potential_id")
mobility = models.ForeignKey(Mobility, db_column="mobility_id")
currency = models.ForeignKey(Currency, null=True, db_column="currency_id")
grade = models.ForeignKey(Grade, db_column="grade_id")
position = models.OneToOneField(Position, to_field='positionId', null=True,
blank=True, db_column="position_id")
employeeId = models.CharField(max_length=200, unique=True, db_column="employeeid")
dateInPosition = models.DateField(null=True, db_column="dateinposition")
现在,我希望每个员工都能得到职位头衔、姓名,以及每个继任任期中该员工的职位在继任者表中出现的次数,以及每个员工在继任者表中出现的次数。最重要的是,我想在一个单独的查询或更具体地说,一个Django ORM语句中完成所有这些,因为我是以分页的方式完成的,但我希望能够对这些列中的任何一列的结果进行排序
到目前为止,我有:
emps = Employee.objects.all()
.annotate(ls_st=Count('succession__term'))
.filter(succession__term__description="ShortTerm")
.order_by(ls_st)
.prefetch_related('person', 'position')[lower_limit:upper_limit]
这只是继承术语中的一个,我想通过添加更多注释调用将其扩展到所有术语。
我的问题是过滤器调用在整个查询中都起作用。我只想筛选计数呼叫
我试过做一些类似CountSessional\uuuuu term\uuuuuu description'=短期的事情,但这不起作用。还有别的办法吗
事先非常感谢,
问候,
Linus那么你想要的是每种不同类型的继承术语的计数?这是相当复杂的,我认为你现在不能用内置的django orm来实现这一点。除非你做了一个额外的查询 在django 1.8中,我相信您将能够使用新的查询表达式来实现这一点。但是1.8当然还没有发布,所以这对你没有帮助 同时,您可以使用非常方便的django aggregate if包 使用django aggregate if,您的查询可能如下所示:
emps = Employee.objects.annotate(
ls_st=Count('succession__term', only=Q(succession__term__description="ShortTerm")),
ls_lt=Count('succession__term', only=Q(succession__term__description="LongTerm")), # whatever your other term descriptions are.
ls_ot=Count('succession__term', only=Q(succession__term__description="OtherTerm"))
)
.order_by('ls_st')
.prefetch_related('person', 'position')[lower_limit:upper_limit]
免责声明:我从未使用过django aggregate if,所以我不完全确定这是否有效,但根据自述文件,它似乎应该有效