How to get all users of a group in Django? - django

I want to get a list of all users who are within a Django Group. For example:
User.objects.filter(group='Staff')
I cannot find how to do this query anywhere in the docs.

The following query solved my problem.
User.objects.filter(groups__name='Staff')
Thanks to #SardorbekImomaliev for figuring it out.

This query allows you to find users by group id rather than by group name:
group = Group.objects.get(id=group_id)
users = group.user_set.all()
Here's a query that lets you search by group name:
users = User.objects.filter(groups_name='group_name')

Related

Group by query in Django

I am still in the same problem. I hope you cooperate in the solution. I want to get the result of this query in Django.
SELECT city_name, MAX(total_places) total_places
FROM(SELECT city city_name, COUNT(city) total_places
FROM Places
GROUP BY 1) t1
GROUP BY 1;
If I look at this answer, it should be something like:
Places.objects.annotate(total=COUNT('city')).latest('total')

Django-ORM: Count number of groups of each user

I know that I can get the number of groups each user has with this query:
User.objects.filter(groups__in=Group.objects.all()).annotate(Count('pk'))
But something is missing:
The users which are in no group at all.
How can I use the django orm to get all users annotated by their group count, incluse users with no group?
You can use Count method with groups attribute:
from django.db.models import Count
User.objects.annotate(group_count=Count('groups'))

Django 1.7 Groups

What would be the best method for altering the default group to include a foreign key to a company?
This is using the built - in auth, or would you create a custom auth?
To further update... I would like a many - to - many between groups and company and then a third table for user groups. For example -- company groups (m2m) and user companygroups (m2m)
This will allow organizations to create their own groups if needed...
I have decided to create a m2m between the company table and groups table

How to use Group by clause in django with the filter query

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
Use can use raw to make group by
http://doughellmann.com/2007/12/using-raw-sql-in-django.html

How can I get GROUP BY to work the Django ORM where I want all fields and the object

I need to group all entries by user and get the count doing something like this:
class Promotion(models.Model):
pass
class Entry(models.Model):
user = models.ForeignKey('User');
promotion = models.ForeignKey('Promotion')
def get_uniques(promotion_id):
promotion = Promotion.objects.get(promotion_id)
entries = promotion.entry_set.annotate(Count('user'))
return entries
However it's returning the same user multiple times. I've also tried the following after looking around StackOverflow, and it seem to be doing something other than what I want:
promotion.entry_set.annotate(Count('user')).order_by('user')[:10]
promotion.entry_set.all().values('user').annotate(entry_count=Count('user')).order_by()
Entry.objects.filter(promotion=promotion).annotate(Count('user')).order_by('user')
Basically I'm trying to do this, giving me an Entry object for each user:
Entry.objects.raw("""
SELECT *
FROM promotion_entry
WHERE promotion_id = %s
GROUP BY user_id""", (promotion_id,))
Then I'll perform a second query to get the entry count, still not ideal. Can I do a GROUP BY without raw?
There seem to be a ticket that would let me do what I want in the future over on the bugtracker by enabling DISTINCT ON: https://code.djangoproject.com/ticket/6422
If you want to count entries for each user use:
promotion.entry_set.all().values('user').annotate(entry_count=Count('id')).order_by()
If you want some entry for each user use:
promotion.entry_set.all().values('user').annotate(entry_id=Max('id')).order_by()
This will give you id's of entries, use __in to get objects themselves.