1

I went through a lot of links and sites , but i can't find the solution for my problem anywhere. So at last i came here .

My problem is that, I want to use the group by clause with the filter query.

i have found this

How to query as GROUP BY in django?

for objects.all() . But could not find anything for objects.filter()

Here is my query

query =Kicthen.objects.filter( cabinets='1').query

query.group_by = ['style_id']

results = QuerySet(query=query, model=Kicthen)

But its return nothing.

I don't want to use any annotate and distinct with it

Community
  • 1
  • 1
Sakeer
  • 1,885
  • 3
  • 24
  • 43
  • 1
    What are you trying to by grouping object? Where is your aggregate function? – Siva Arunachalam Sep 19 '13 at 05:06
  • You can use aggregation https://docs.djangoproject.com/en/dev/topics/db/aggregation/#topics-db-aggregation – Sudip Kafle Sep 19 '13 at 05:31
  • is this compulsary to use aggregate function with the group by clause. can't i use the group by clause to get the distinct vlaues? – Sakeer Sep 19 '13 at 05:32
  • If you group your results by a specific column, you need to do something with the other columns. If you just want the distinct values, use `.values_list('style_id', flat=True).distinct()`. – Thierry J. Sep 19 '13 at 06:18
  • i have mentioned that i don't want to use dictinct – Sakeer Sep 19 '13 at 06:30
  • i have found this http://stackoverflow.com/questions/629551/how-to-query-as-group-by-in-django?rq=1 for objects.all() . But could not find anything for objects.filter() – Sakeer Sep 19 '13 at 10:39

1 Answers1

1

Use can use raw to make group by http://doughellmann.com/2007/12/using-raw-sql-in-django.html

  • I only show another option. Give that comment to the creator of the raw command – Manuel Pomares Sep 26 '13 at 23:49
  • @yuvi What's so scary about SQL? Also if you know a better solution it would be nice to add it as an answer. – redbmk Nov 21 '16 at 12:36
  • @redbmk but seriously though. SQL is terrible, just... So, so bad. There's a reason people built an entire framework with a feature dedicated to *avoiding it*. Other than raw, I'd say aggregation is the simple solution but OP doesn't seem to be having it (see comments above) so there aren't really that many other options. – yuvi Nov 22 '16 at 21:20