Django 查询使用 unique 和 order_by

发布于 2024-11-19 04:54:31 字数 970 浏览 4 评论 0原文

我有两个模型

class Employer(models.Model):
     name = models.CharField(max_length=300, blank=False)
     id = models.IntegerField()
     status = models.IntegerField()
     eminence = models.IntegerField(null=False,default=4)

class JobTitle(models.Model)
     name = models.CharField(max_length=300, blank=False)
     employer = models.ForeignKey(Employer,unique=False,null=True)
     activatedate = models.DateTimeField(default=datetime.datetime.now)

,我需要所有雇主按照其职位最后激活的顺序排列。

Employer.objects.filter(status=1).order_by('eminence','-jobtitle__activatedate')

这个查询给了我我想要的东西,但如果雇主有多个职位,它会返回重复的雇主。

我会使用 distinct() 但在 Django 文档中我发现

*order_by() 调用中使用的任何字段都包含在 SQL SELECT 列中。当与distinct()结合使用时,有时会导致意想不到的结果。如果您按相关模型中的字段排序,这些字段将添加到选定的列中,并且它们可能会使重复的行看起来不同。由于额外的列不会出现在返回的结果中(它们仅用于支持排序),因此有时看起来像是返回了不不同的结果。*

尽管他们解释了我的问题,但没有指定解决方案。

能给我一个建议,如何在不破坏 API 稳定性的情况下按雇主列表进行分组?

I have two models

class Employer(models.Model):
     name = models.CharField(max_length=300, blank=False)
     id = models.IntegerField()
     status = models.IntegerField()
     eminence = models.IntegerField(null=False,default=4)

class JobTitle(models.Model)
     name = models.CharField(max_length=300, blank=False)
     employer = models.ForeignKey(Employer,unique=False,null=True)
     activatedate = models.DateTimeField(default=datetime.datetime.now)

I need all employers in the order of whose jobtitle activated last.

Employer.objects.filter(status=1).order_by('eminence','-jobtitle__activatedate')

This query gives me what I want but it returns repeated employers if employer has more than one jobtitle.

I would use distinct() but in Django documents I found that

*Any fields used in an order_by() call are included in the SQL SELECT columns. This can sometimes lead to unexpected results when used in conjunction with distinct(). If you order by fields from a related model, those fields will be added to the selected columns and they may make otherwise duplicate rows appear to be distinct. Since the extra columns don't appear in the returned results (they are only there to support ordering), it sometimes looks like non-distinct results are being returned.*

Although they explained my problem no solution is specified.

Could give me a suggestion how can I group by my employer list without corrupting the API stability?

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

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

发布评论

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

评论(1

壹場煙雨 2024-11-26 04:54:31

您可以将其最新的 JobTitle 激活日期放在 Employer 类上,然后按此字段排序,而不使用关系。[1]这里的权衡是一些数据重复,并且当相应的 JobTitle 实例发生变化时,需要手动更新雇主的最新职位激活日期。

另外,检查 这篇文章提供了另一个使用annotate()的解决方案。

相关问题:

[1] Queryset API unique() 不起作用?< /a>

You can place on the Employer class its latest JobTitle activation date, and then order by this field without using relations.[1] The tradeoff here is a bit of data duplication, and the necessity to update employer's latest job title activation date manually when corresponding JobTitle instance changes.

Also, check this post for another solution which uses annotate().

Related questions:

[1] Queryset API distinct() does not work?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文