Here I have a queryset where I am using raw SQL to get data.
But this approach is not fulfilling my requirements.
I want to convert this query into pure Django ORM query
queryset = Model.objects.raw("select id,category,count(category)
as total_orders, count(distinct ra, category) as order_type,
SUM(CASE WHEN service_status = 'success' THEN 1 ELSE 0 END) as total_success_order,
SUM(CASE WHEN service_status = 'failed' THEN 1 ELSE 0 END) as total_failed_order
from table
group by ra;")
How can I do this?
HOW CAN WE IMPLEMENT count(distinct ra, category) as order_type in queryset?
My approach
queryset = Model.objects.values('ra').annotate(category=F('category'),\
order_type=Count('ra', 'category', distinct=True),total_orders=Count('category'),\
total_success_order=Count('service_status', filter=Q(service_status='success')),\
total_failed_order=Count('service_status', filter=Q(service_status='failed'))).order_by()
Problem is how to get the order_type because of this need calculation on other attribute values ... as order_type=Count('ra', 'category', distinct=True) is giving error