views:

639

answers:

1

Hi, thats my code:

##### models.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'

In my 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)
...

Before I switched to ForeignKey, it worked with:

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

How to 'join' these two QuerySets to one QuerySet and make a distinct()?

Thanks!

+2  A: 

Hi,

I am not familiar with geodjango, but combining QuerySets into one QuerySet is possible via the Q-Object and Boolean Operators. See http://docs.djangoproject.com/en/dev/topics/db/queries/#complex-lookups-with-q-objects

Example:

Q(p1_points) | Q(p2_points)

I can't help you further, because I am not really sure what you are trying to accomplish.

stefanw
I want to get all SinglePoint instances in my Project without double SinglePoint instances (distinct). It was no problem with a ManyToMany-Field, because then I accessed them with .filter(connection__vektordata__ ...).distinct() and now I have two Filds: .filter(p1_set__vektordata__ ... and .filter(p2_set__vektordata__ ...)
Sorry, I meant "Fields" not "Filds"
results = Q(p1_points) | Q(p2_points)results.children gives me [[<SinglePoint: A>, <SinglePoint: X1>], [<SinglePoint: X1>, <SinglePoint: X2>]] but I wish to have [<SinglePoint: A>, <SinglePoint: X1>, <SinglePoint: X1>, <SinglePoint: X2>] to call distinct()