Sandeep
Sandeep

Reputation: 629

group by with all fields django

I want to run 2 queries in django, I am using mysql, Please help me

first one

SELECT * FROM `invitations` 
WHERE post_id = 19215 GROUP BY (user_id) 

it is not group by from user_id

   data = Invitations.objects.filter(post_id=19215).annotate(user_id=Count('user_id'))

now i have add value

data = Invitations.objects.values('user_id').filter(post_id=19215).annotate(user_id=Count('user_id'))

it return me not all fields select *

data = Invitations.objects.values('user_id', 'xyz').filter(post_id=19215).annotate(user_id=Count('user_id'))

it group by user_id and xyz

Please give me solution

and second is

SELECT *, GROUP_CONCAT(interview_mode) FROM invitations WHERE post_id = 19215 GROUP BY (user_id)

Upvotes: 1

Views: 85

Answers (1)

Jaskaran singh Rajal
Jaskaran singh Rajal

Reputation: 2330

Run this:

query= "SELECT *, GROUP_CONCAT(interview_mode) 
FROM invitations WHERE post_id = 19215 
GROUP BY (user_id)"

data = Model.objects.raw(query)

Upvotes: 1

Related Questions