Saturday, 15 March 2014

python - Usage of .annotate() in Django queryset with .extra() expression -



python - Usage of .annotate() in Django queryset with .extra() expression -

using model:

class mytable(models.model): name = models.charfield(max_length=255,) avg = models.decimalfield(max_digits=5, decimal_places=2) var = models.decimalfield(max_digits=5, decimal_places=2)

i run next query in postgresql:

select id, name, max(case when id = '1' avg end) avg1, max(case when id = '1' var end) var1, max(case when id = '2' avg end) avg2, max(case when id = '2' var end) var2 app_mytable grouping name

my question how can query in django? wrote queryset dont have write "group by" expression.or in other hand usage of annotate in case?

q = mytable.objects.extra(select= {"avg1":"max(case when id = 1 avg end)", "var1":"max(case when id = 1 var end)", "avg2":"max(case when id = 2 avg end)", "var2":"max(case when id = 2 var end)" }).annotate(?)

python django group-by django-queryset

No comments:

Post a Comment