gpt4 book ai didi

python - 如何将 2 个 Django 查询集合并为一个并创建一个 SELECT DISTINCT

转载 作者:行者123 更新时间:2023-12-03 01:25:30 25 4
gpt4 key购买 nike

模型.py
class SinglePoint(models.Model):
attributes = models.TextField(blank=True)
name = models.CharField(max_length=100)
geom = models.PointField() #Kartenposition
objects = models.GeoManager()

class Connection(models.Model):
name = models.CharField(max_length=100)
#points = models.ManyToManyField(SinglePoint) #OLD
p1 = models.ForeignKey(SinglePoint, related_name='p1_set') #NEW
p2 = models.ForeignKey(SinglePoint, related_name='p2_set') #NEW
obs = models.ManyToManyField(Observation, blank=True)
conds = models.ManyToManyField(Condition, blank=True)
objects = models.GeoManager()

class Meta:
order_with_respect_to = 'p1'

在我看来.py:

...
p1_points = SinglePoint.objects.filter(name=name)
p2_points = SinglePoint.objects.filter(name=name)
...

在我切换到ForeignKey之前,它适用于:

points = SinglePoint.objects.filter(name=name)

如何将这两个查询集“连接”到一个查询集并创建一个distinct()?

谢谢!

最佳答案

我花了一段时间才找到这个

all_points = p1_points | p2_points

关于python - 如何将 2 个 Django 查询集合并为一个并创建一个 SELECT DISTINCT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1125844/

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