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'
在我的view.py中:
...
p1_points = SinglePoint.objects.filter(p1_set__vektordata__order__project__slug=slug)
p2_points = SinglePoint.objects.filter(p2_set__vektordata__order__project__slug=slug)
...
在我切换到ForeignKey之前,它适用于:
points = SinglePoint.objects.filter(connection__vektordata__order__project__slug=slug)
如何将这两个QuerySet连接到一个QuerySet并创建一个distinct()?
谢谢!
答案 0 :(得分:63)
我花了一段时间才找到这个
all_points = p1_points | p2_points
答案 1 :(得分:8)
我不熟悉geodjango,但可以通过Q-Object和Boolean Operators将QuerySets组合成一个QuerySet。见http://docs.djangoproject.com/en/dev/topics/db/queries/#complex-lookups-with-q-objects
示例:
Q(p1_points) | Q(p2_points)
我无法帮助你,因为我不确定你想要完成什么。
答案 2 :(得分:6)
我认为Q queries可以实现您的需求:
points = SinglePoint.objects.filter(
Q(p1_set__vektordata__order__project__slug=slug) |
Q(p2_set__vektordata__order__project__slug=slug)
).distinct()
答案 3 :(得分:0)
p1_points.union(p2_points)
请参见Docs