Django: Where should I put queries about User which is in relation with other models
<p>I am writing a Django app which define groups of user. A user can be part of many groups.</p>

<p>I want to be able to retrieve, from a list of groups, users which are members of at least one of those groups and with no duplicate.</p>

<p>So i wrote a query to accomplish that. That method will take a list of group_id and simply returns a list of users.
My question is: where is the best place to put that method?</p>

<p>I was thinking to create a custom manager for my Group model. But it don't seems logical as my method will return to me a list of Users.</p>

<p>According to the doc: <a href="https://docs.djangoproject.com/en/1.8/topics/db/managers/" rel="nofollow">https://docs.djangoproject.com/en/1.8/topics/db/managers/</a></p>

<p>Manager of a model should contains queries about that model.</p>

<p>After some research, I've seen that is not advised to extends the django.auth.models.User manager (at least in my case).</p>

<p>For the moment, i will just put my method in a 'service.py' file, but I was wondering if there is not a more elegant way to accomplish what i am trying to do.</p>

<p>Thanks by advance for your suggestions!</p>

<p>In your views.py you can use the below function and call it in your view:</p>

<pre><code>def get_groups_users(*groups):
    users = User.objects.filter(groups__name__in=list(groups))
    return list(set(users))
</code></pre>

python django django-models django-orm django-managers