美好的一天! 我是这里的新手,也是django的初学者,我希望在注册成功后立即创建用户注册。
resources.py
class AccountResource(ModelResource):
class Meta:
proxy=True
authorization= DjangoAuthorization()
# authentication = AUTHENTICATION
queryset = User.objects.all()
list_allowed_methods = ['get', 'post']
resource_name = 'account'
fields = ['username','password','email']
def prepend_urls(self):
return [
url(r"^(?P<resource_name>%s)/register%s$" %
(self._meta.resource_name, trailing_slash()),
self.wrap_view('register'), name="api_register"),
url(r"^(?P<resource_name>%s)/login%s$" %
(self._meta.resource_name, trailing_slash()),
self.wrap_view('login'), name="api_login"),
url(r"^(?P<resource_name>%s)/logout%s$" %
(self._meta.resource_name, trailing_slash()),
self.wrap_view('logout'), name="api_logout"),
url(r"^(?P<resource_name>%s)/activate%s$" %
(self._meta.resource_name, trailing_slash()),
self.wrap_view('activate'), name="api_activate"),
]
def activate(self, request, **kwargs):
data = self.deserialize(request, request.body,format=request.META.get('CONTENT_TYPE', 'application/json'))
activation_key = data.get('activation_key', '')
activated_user = RegistrationProfile.objects.activate_user(activation_key)
if activated_user:
return self.create_response(request, {
'success': True,
'username': activated_user
})
else:
return self.create_response(request, {
'success': False,
'reason': 'already activated or user activation not exist',
}, HttpUnauthorized )
def register(self, request, **kwargs):
data = self.deserialize(request, request.body, format=request.META.get('CONTENT_TYPE', 'application/json'))
username = data.get('username', '')
password = data.get('password', '')
email = data.get('email', '')
RegistrationProfile.objects.create_inactive_user(username, email, password,'',send_email=True)
new_user = authenticate(username=username, password=password)
if new_user:
return self.create_response(request, {
'success': True
})
else:
return self.create_response(request, {
'success': False,
'reason': 'incorrect',
}, HttpUnauthorized )
def default_group(sender, instance, created, **kwargs):
if created:
instance.groups.add(Group.objects.get(name='Member'))
post_save.connect(default_group, sender=User)
以上代码用于使用带有原始数据的url POST注册,激活等。在使用register创建用户后或激活用户后,如果有办法创建“Profile”,我该知道什么。任何回应或关注都受到赞赏。提前谢谢。
答案 0 :(得分:0)
<强>东西/ Models.py 强>
from django.db.models.signals import post_save
class Profile(models.Model):
(blah blah blah) ...
def create_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
post_save.connect(create_user_profile, sender=User)
我之前的问题是我没有删除我的数据库(因为我担心它会导致更大的问题)。我不知道,但是一旦我这样做,它的工作就像一个魅力(现在我的问题是我需要重新编码我的数据XD)。