python - Advanced database query in Django -



python - Advanced database query in Django -

i have table(model) in database this:

name|avarage|evaluation

marco| 9.6 | 1

marco| 9.3 | 2

kevin | 8.8 | 1

kevin | 9.4 | 2

so need info show table @ template in way:

name | eval1 | eval2

marco | 9.6 | 9.3

kevin | 8.8 | 9.4

how can create query in view?

there no easy way using orm. seek using itertools.groupby:

from itertools import groupby results = modelcls.objects.order_by("name") grouped = groupby(results, lambda r: r.name)

you have models grouped name:

for name, objects in grouped: # process model objects in each grouping obj in objects: # each object

passing groupby object template should work fine, aware it's generator , exhausted after iterate on it.

python django orm django-models django-views

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -