Home > Blockchain >  Django Raw Query does not Output
Django Raw Query does not Output

Time:12-26

views.py

def dashboard(request):
    employee = Employee.objects.count()
    position1 = Posit.objects.raw('SELECT employee.stat, count(posit.position_id) as NO FROM employee, posit WHERE employee.id = posit.employee_id AND posit.position_id="1" GROUP BY employee.stat')
    
    context = {
        'employee ': employee ,
        'posit ': posit ,
        
    }
    return render(request, 'dashboard/dashboard.html', context)

I can output the employee count by using {{employee}} but when I use {{posit}} the position output is "RawQuerySet". This is the output in MariaDb using the raw query.

employee.stat NO
1 100
2 20
3 30

How can I output all the value of each employee.stat in the dashboard.html? Also can I output each employee.stat NO seperately?

CodePudding user response:

You can iterate through the RawQuerySet according to the documentation, so your code needs to be along these lines:

context = {
        'employee ': employee ,
        'posit': [pos for pos in position1],
        
    }

Then, you can iterate over the posit variable in a template tag such as:

{% for pos in posit %}

and then use {{pos}} when you need it.

  • Related