Django group by Count함수, order by(정렬)

2021. 4. 3. 14:00파이썬/Django

쿼리

select char,count(char) total from Trained_Box
where status='registered'
group by char
order by total desc

결과

def dashboard(request) :
    template = 'ocr/dashboard.html'
    chars = Trained_Box.objects.filter(status='registered').values('char').annotate(total=Count('char')).order_by('-total')
    print(chars)
    results = {
        'results' : chars
    }
    print(results)
    return render(request, template, results)