我有两张表叫位置和价格。你知道吗
费率表包含以下项目:
von_location是location表的外键。你知道吗
我想要一个通缉率的位置。如果我用浪漫的2
搜索一个地点,我得到的是id为5
的地点。这很好。但是如果我也用浪漫的3
搜索一个位置,我也会得到id 5
的位置。但我想要这样的逻辑:
如果一个地点有两种价格,请选择最新的。我在django怎么做?你知道吗
我的模型是:
class Location(models.Model):
name = models.TextField()
adres = models.TextField()
class Rate(models.Model):
von_location = models.ForeignKey(Location,related_name='locations_bewertung')
bewertung = models.IntegerField(max_length=2)
romantic = models.IntegerField(max_length=2)
priceleistung = models.IntegerField(max_length=2)
datum = DateTimeField(auto_add_now=True)
我删除了一些与我的问题无关的专栏
我试过这个:
locations = Location.objects.filter(**s_kwargs).order_by('locations_bewertung').distinct('locations_bewertung')
两个查询的位置仍然相同。2
和3
。你知道吗
如果我理解正确,您需要的是向位置模型添加一个函数,如下所示:
然后你可以这样做:
我不知道你是想在
Location
还是Rate
上过滤。您的介绍意味着您希望在Rate
上进行筛选(因为romantic
属于该模型),但是您的示例查询正在Location
上进行筛选。你知道吗如果要查找与一组查询参数关联的最新
Location
的Rate
,请尝试以下操作:相关问题 更多 >
编程相关推荐