Thursday, April 17, 2014

Re: How to create Union in Django queryset

On 4/16/14 11:51 PM, Shoaib Ijaz wrote:
I don't want use SQL query in django
OK. You *could* have class B inherit from class A:

class B(A):
  link = models.IntegerField(blank=True, null=True)

and then do your query on A:

ulist = A.objects.all()

hth

                            - Tom


On Thursday, 17 April 2014 04:39:09 UTC+5, Russell Keith-Magee wrote:
On Wed, Apr 16, 2014 at 9:30 PM, Shoaib Ijaz <shoaib...@gmail.com> wrote:
> Sorry for duplicate post How to create Union
>
> 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

No comments:

Post a Comment