Thursday, January 2, 2020

Re: get user that is member of two different group

I don't think that's what the he asked for. It will return users who are members of either test1 OR test2, but I read the question as returning users who are members of both test1 AND test2. I think the proper query would be:

User.objects.filter(groups__name='test1').filter(groups__name='test2')

That does two joins to the groups table and filters the first on 'test1' and the second on 'test2'.

On Thu, Jan 2, 2020 at 2:34 AM Lunga Baliwe <lunga001@gmail.com> wrote:
Hi there,
maybe you can use  something like User.objects.filter(groups__name__in=['test1', 'test2']).
Also check https://docs.djangoproject.com/en/3.0/ref/models/querysets/#in for examples of using __in

On Thu, Jan 2, 2020 at 6:49 AM sum abiut <suabiut@gmail.com> wrote:
Hi,
I have two separate groups, for example, test1, and test2. I want to get a user that is a member of group test1 and group test2.

I've try User.objects.filter(groups__name='test1') to get users of  group test1. not sure how to check if a user belongs to group test1 and is also belong to group test2

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAPCf-y6F2rbgG1%3DgK4QX8FDKDq61b8mVdvtrtv5LwQQmfwhY3g%40mail.gmail.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CANDnEWeuXRnMqCiS8co%3DhifuUhC8NrYo8e1j8Y_EVF6r1sBwew%40mail.gmail.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAD4ANxXLwZ9WgbMGfNWOSA%2BeUP7FxFHDgkpwUBVcmqH8gaGK8g%40mail.gmail.com.

No comments:

Post a Comment