I don't want use SQL query in django

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 
> <[email protected]<javascript:>> 
> 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 
> > 
> > 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 
>
> Why not? That would seem to be the exact answer you need. 
>
> You appear to have a very specific idea of the SQL query you want to 
> issue, including UNION clauses (an operator that Django's ORM doesn't 
> support) and "extra" columns (which means the data you want is outside 
> your regular Django model). Getting this query in 100% native Django 
> query sets is going to be somewhere between difficult and impossible. 
>
> However, you don't have to drop right back to SQL cursors - Django has 
> raw query sets for exactly this purpose: 
>
> ABWrapper.objects.raw("SELECT …. FROM … WHERE") 
>
> This will return ABWrapper objects contained in a query set object 
> that should be compatible with Django REST Framework, but you get 
> those objects by providing the exact SQL you want to execute. 
>
> See: 
>
> https://docs.djangoproject.com/en/dev/topics/db/sql/ 
>
> for more details 
>
> Yours, 
> Russ Magee %-) 
>

-- 
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 [email protected].
To post to this group, send email to [email protected].
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/3cd4534c-aee8-4d94-9efa-5500ded935f5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to