union is not working in django rest framwork? -


i have 2 table , want implement union on result set? have crated 1 parent model , include pk fk in both child tables , implement following quires in rest framework

q1 = parent.objects.filter(chile_1__isnull=true).filter(chile_1__name='blog').values('chile_1__id').extra(select = {"id":"chile_1__id"})      q2 = parent.objects.filter(chile_2__isnull=true).filter(chile_2__name='blog1').values('chile_2__id').extra(select = {"id":"chile_2__id"}) 

and have create alias of both children containing different fields name when run following union

q3 =  q1 | q2 

showing following error

typeerror: merging 'geovaluesqueryset' classes must involve same values in each case.

will 1 tell me how values of children using parent? thanks.

you use model serializer. in order serialize 2 or more different models need write non-model based serializer. in api both , append them using itertools.chain bind them , convert result list. pass list serializer... et voilĂ .

hope helps - in rest docs.

edit: example:

      class mycustomserializer(serializers.serializer):         object_pk = serializers.integerfield(source='pk')         name = serializers.charfield()         goloc = serializers.charfield(source='get_geoloc')      class mycustomviewset(viewsets.modelviewset):         serializer_class = mycustomserializer         def list(self, request):             mylist = []             # q1 , q2 querysets need still fetched...             o in q1:                 mylist.append(o)             o in q2:                 mylist.append(o)             queryset = mylist             serializer = mycustomserializer(queryset, many=true)             return response(serializer.data)  

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

inno setup - TLabel or TNewStaticText - change .Font.Style on Focus like Cursor changes with .Cursor -