[Fixed]-Union query from Django ORM

1πŸ‘

βœ…

On Django, you can use | to join two querysets but I’m not using it here.

Because values/annotate actually returns a list of tuples instead of a query_set

you can run raw SQL on Django but raw is for optimization. https://docs.djangoproject.com/en/1.9/topics/db/sql/

object_count = Elements.objects.all().values('element_type').annotate(total=Count('element_type'))
response_data = {}
for q in object_count:
    if q['element_type'] == 'xyz':
        response_data['total_ xyz'] = q['total']
    if q['element_type'] == 'pqr':
        response_data['total_ pqr'] = q['total']
response_data['users_count'] = MyUser.objects.all().count()

Leave a comment