gpt4 book ai didi

python - django unique() 和 order_by() extra() 一起使用

转载 作者:太空宇宙 更新时间:2023-11-03 17:58:54 26 4
gpt4 key购买 nike

我使用 django Restframwork

这是链接到127.0.0.1/movie/?top=1时views.py中的函数

我想查询还在上映的电影以及1个月内的上映日期但有时结果会重复,
请帮我检查一下我的查询为什么会发生这种情况?

def top(self):
past_month.date() = '2014-12-20'
today.date() = '2015-01-20'
self.queryset = Movie.objects.filter(releaseday__gte=past_month.date(),releaseday__lte=today.date(),movie__showtime__gte=today).extra({'vrank': 'CASE WHEN hot=0 THEN 21 WHEN hot >=10 THEN 21 ELSE hot END'}).order_by('vrank','-releaseday').distinct()

这是我的模型:

class Movie(models.Model):
title = models.CharField(max_length=255, blank=True)
releaseday = models.DateField( null=True,blank=True)

class MovieShowtime(models.Model):
movie = models.ForeignKey( Movie, null=True,blank=True,related_name = 'movie' )
movietime = models.DateTimeField(null=True,blank=True)

最佳答案

您的查询可能返回不同的行,但某些行之间的差异仅存在于您不打算查看的列中(例如,相关表中的字段)。

为了确保您的查询返回您关心的列的不同值,请在distinct()之前使用.values(['col1', 'col2' ...])。

另请务必查看此处文档中有关对值()中未包含的列进行排序的注释:https://docs.djangoproject.com/en/1.7/ref/models/querysets/#distinct

关于python - django unique() 和 order_by() extra() 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28035761/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com