from django.views.generic.edit import CreateView
from django.contrib.auth.models import User
from django.contrib.auth.models import Group
from django.core.urlresolvers import reverse
class UserCreate(CreateView):
model = User
fields = ['username'] #only expose the username field for the sake of simplicity add more fields as you need
#this one is called when a user has been created successfully
def get_success_url(self):
g = Group.objects.get(name='test') # assuming you have a group 'test' created already. check the auth_user_group table in your DB
g.user_set.add(self.object)
return reverse('users') #I have a named url defined below
我假设您希望将新创建的用户自动添加到现有的组中。如果我错了,请纠正我,因为你的问题中没有提到这一点。在
这是我在视图.py
在我的网址.py:
^{pr2}$我在django1.8中测试了这一点(我确信它在1.7中也可以工作)。我验证了组关系是在auth_user_group表中创建的。在
另外,我还发现了这个:https://github.com/tomchristie/django-vanilla-views/tree/master这可能对你的项目有用。在
相关问题 更多 >
编程相关推荐