Wednesday, July 23, 2014

Re: Getting list of user groups


On Thu, Jul 24, 2014 at 4:39 AM, Kyle Adams <kyle@n3twork.com> wrote:
I'm trying to get a list of the user groups available in a system. I have groups set up, but how do I, on some request, get a list of the names of each of the groups in my system?

Hi Kyle,

It's bit difficult to answer your question, because it's not clear what sort of answer you're looking for. 

At the high level, Django's database query API will let you access this information: 

from django.contrib.auth.models import Group
groups = Group.objects.all()

will return a queryset populated with all the group objects that have been defined in your database. Once you pick a particular group (e.g., the first one):

group = groups[0]

you can then get all the users associated with that group:

users = group.user_set.all()

This will be a list of User objects who all belong to the selected group.

If you're just looking for the *names* of the groups, you could do something like:

groups = Group.objects.value_list('name', flat=True)

which will return a list of *just* the names of the groups in your database. Or, you could do something like:

groups = Group.objects.values('id', 'name')

will return a list of dictionaries; each dictionary will have the keys "id" and "name", reflecting the primary key and the name of each group. This is the sort of data you might use to populate a widget in a user interface.

However, this is purely at the query level. if you're looking to build a user interface, you may not need to interact at this level.
 
For some context, I'm working on an analytics dashboard, and part of what I want to be able to do is look at the data for certain groups, perhaps staff and non-staff. To do this, I want to be able to ask Django for the list of groups first, so the user can choose a group from there, and then later will get a list of all users in that group, so I can look only at their data.

This is a bit of an open question - it's like saying "how do I build a car?". What sort of car do you want to build? A sporty hatchback? A pickup truck? An SUV? Each of them represents a different way of solving the "being a car" problem, and other than the basic "It's got four tires, a steering wheel and an engine" aspect, each of them are very different in construction, handling and so on. 

Bringing it back to your problem - are you looking to build a simple website where you're navigating between pages? Or a heavily AJAX driven site where selecting one widget affects the data displayed on other parts of the page? 

Getting the *data* is the easy part - that's what I described above. But how you expose that data to the end user will depend much more on your intended user interface and user experience.

The simplest approach would be the navigation based approach - the user lands on a page with a list of groups; they click on a group, and get taken to a page with a list of users in that group. In that case, the approach is almost exactly the same as the tutorial uses - just substitute "Group" for "Poll", and "User" for "Question". 

If you want a dynamic, AJAX-driven approach - well, that requires a bit more than can be easily explained over an email. 

Yours,
Russ Magee %-)


--
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 post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAJxq84_WfDJ_a2X%2BH7sba2ucy3g%2BC9z%3DgS-nVOXvWnCrrXhE5w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment