two sets querysets queryset query example different combined and django distinct geodjango django-queryset

django - sets - queryset and queryset



Cómo fusionar 2 Django QuerySets en uno y hacer un SELECT DISTINCT (3)

Creo que las http://docs.djangoproject.com/en/dev/topics/db/queries/#complex-lookups-with-q-objects pueden lograr lo que necesitas de esta manera:

points = SinglePoint.objects.filter( Q(p1_set__vektordata__order__project__slug=slug) | Q(p2_set__vektordata__order__project__slug=slug) ).distinct()

modelos.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''

En mi vista.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) ...

Antes de cambiar a ForeignKey, trabajó con:

points = SinglePoint.objects.filter(connection__vektordata__order__project__slug=slug)

¿Cómo ''unir'' estos dos QuerySets a un QuerySet y hacer un distinto ()?

¡Gracias!


Me tomó un tiempo encontrar esto

all_points = p1_points | p2_points