I recently needed to add a large number of users to a permission group in Django. I had a hard time finding a way to do this in the documentation, so I thought I’d share my solution.
from django.contrib.auth.models import Group, User g = Group.objects.get(name='My Group Name') users = User.objects.all() for u in users: g.user_set.add(u) |
Easy as pie. I originally attempted to do this from the perspective of a user, but as it turns out, doing it from the group perspective is much easier.