python - Add users to groups in Django -
i using group module in django.
i've created views groupcreateview
, groupupdateview
in can update permissions , group name, want add users groups.
right have update each user object , set groups belongs. want other way around create groups , add users group.
how obtained? guess it's group.user_set.add(user)
i assuming want add newly created user existing group automatically. correct me if wrong since not stated in question.
here have in views.py
from django.views.generic.edit import createview django.contrib.auth.models import user django.contrib.auth.models import group django.core.urlresolvers import reverse class usercreate(createview): model = user fields = ['username'] #only expose username field sake of simplicity add more fields need #this 1 called when user has been created def get_success_url(self): g = group.objects.get(name='test') # assuming have group 'test' created already. check auth_user_group table in db g.user_set.add(self.object) return reverse('users') #i have named url defined below
in urls.py:
urlpatterns = [ url(r'list$', views.userlist.as_view(), name='users'), # have list view show list of existing users ]
i tested in django 1.8 (i sure works in 1.7 well). verified group relationship been created in auth_user_group table.
p.s. have found this: https://github.com/tomchristie/django-vanilla-views/tree/master might useful project.
Comments
Post a Comment