此数据库后端不支持DISTINCT ON字段 [英] DISTINCT ON fields is not supported by this database backend
问题描述
我正在使用distinct获取不同的最新值,但这给了我一个错误:
I am using distinct to get the distinct latest values but it is giving me an error:
DISTINCT ON字段不受支持此数据库后端
DISTINCT ON fields is not supported by this database backend
views.py
views.py
class ReportView(LoginRequiredMixin, generic.TemplateView):
template_name = 'admin/clock/report.html'
def get_context_data(self, **kwargs):
context = super(ReportView, self).get_context_data(**kwargs)
context['reports'] = TimesheetEntry.objects.filter(
timesheet_jobs__job_company = self.request.user.userprofile.user_company,
).distinct('timesheet_users')
return context
基本上我想在 TimesheetEntry
模型上查询,其中会有很多条目用户
,它是用户
in-b中的外键uilt模型。
Basically I want to query on TimesheetEntry
model where there will be lot of entries of user
which is a foreign key in User
in-built model.
所以我想用不同的用户进行查询,以便显示该用户的最新条目。对于我来说,获取用户的最新条目非常重要。
So I want to query with distinct user so that latest entry of the user will be displayed. It is very important for me to get the latest entry of user.
models.py
models.py
class TimesheetEntry(models.Model):
timesheet_users = models.ForeignKey(User, on_delete=models.CASCADE,related_name='timesheet_users')
timesheet_jobs = models.ForeignKey(Jobs, on_delete=models.CASCADE,related_name='timesheet_jobs')
timesheet_clock_in_date = models.DateField()
timesheet_clock_in_time = models.TimeField()
推荐答案
distinct ('field_name')
在MySQL中不受支持。它仅支持 distinct()
。 distinct('field_name')
仅在 PostgresSQL 上有效。有关更多详细信息,请检查文档。
distinct('field_name')
is not supported in MySQL. It only support distinct()
. distinct('field_name')
will only work on PostgresSQL. For more details, please check the documentation.
示例(第一个示例之后的示例仅可在PostgreSQL上使用):(从文档复制粘贴:)
>>> Author.objects.distinct()
[...]
>>> Entry.objects.order_by('pub_date').distinct('pub_date')
[...]
>>> Entry.objects.order_by('blog').distinct('blog')
[...]
>>> Entry.objects.order_by('author', 'pub_date').distinct('author', 'pub_date')
[...]
>>> Entry.objects.order_by('blog__name', 'mod_date').distinct('blog__name', 'mod_date')
[...]
>>> Entry.objects.order_by('author', 'pub_date').distinct('author')
[...]
这篇关于此数据库后端不支持DISTINCT ON字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!