Sorry for duplicate post How to create 
Union<http://stackoverflow.com/questions/23110658/how-to-create-union-in-django-queryset>

I am using Django REST Framework in project and I want to create union two 
different Models.

*My Models*

class A(models.Model):
    name = models.CharField(max_length=240, blank=True)
    geometry = models.GeometryField(blank=True, null=True)
    abwrapper= models.ForeignKey(ABWrapper)

    class Meta:
        db_table = 'tbl_a'
class B(models.Model):
    name = models.CharField(max_length=240, blank=True)
    link = models.IntegerField(blank=True, null=True)
    geometry = models.GeometryField(blank=True, null=True)
    abwrapper= models.ForeignKey(ABWrapper)

    class Meta:
        db_table = 'tbl_b'

I am trying to create this query

SELECT id,name FROM tbl_a UNION (SELECT b.id,b.name From tbl_b b)

*My attempt for union*

a = A.objects.values_list('id')
b = B.objects.values_list('id')
queryset = a | b
Error:AssertionError: Cannot combine queries on two different base models.

*Now i tried with parent Model in this way*

class ABWrapper(models.Model):
    objects = models.GeoManager()
    class Meta:
        db_table = u'ab_wrapper'

Added this model as ForeignKey above both Models

a = ABWrapper.objects.filter(a__isnull=False).values('a__id')
b = ABWrapper.objects.filter(b__isnull=False).values('b__id')
queryset = a | b
Error:TypeError: Merging 'GeoValuesQuerySet' classes must involve the same 
values in each case.

*Another attempt by making alias*

a = 
ABWrapper.objects.filter(a__isnull=False).extra(select={'tempID':'a__id'}).values_list('tempID')
b = 
ABWrapper.objects.filter(b__isnull=False).extra(select={'tempID':'b__id'}).values_list('tempID')
queryset = a | b
Error:ValueError: When merging querysets using 'or', you cannot have 
extra(select=...) on both sides.

I have searched on it, mostly answered this issue as using list for both 
models. But I don't want to use list as I am using Django Rest Framework so 
I need QuerySet. So my question if I use list for union can I convert 
resulting list into QuerySet.

*Note:* I don't want to use SQL Query in Django

Is there any other way to do this task?

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/f4182754-b952-41a9-8c4c-90e32a287e06%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to