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

6
  • 1
    What are you trying to by grouping object? Where is your aggregate function? Commented Sep 19, 2013 at 5:06
  • You can use aggregation docs.djangoproject.com/en/dev/topics/db/aggregation/… Commented Sep 19, 2013 at 5: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? Commented Sep 19, 2013 at 5: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(). Commented Sep 19, 2013 at 6:18
  • i have mentioned that i don't want to use dictinct Commented Sep 19, 2013 at 6:30

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

I only show another option. Give that comment to the creator of the raw command
@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 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.