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?