在激活 django-registration 上创建附加模型
Create additional model on activation django-registration
我一直在努力实现不同的答案来处理(例如 Django-Registration & Django-Profile, using your own custom form),但未能在我的项目中使用它,因为它似乎太过时了。
基本上我已经在我的项目中安装了 Django-Registration 并且可以通过它验证和创建用户。
但是,我使用以下 UserProfile 模型扩展了用户:
class UserProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
account_type = models.IntegerField(null = True, default= 1)
daily_message = models.BooleanField(default = True)
tel_number = models.CharField(max_length=20, null = True)
def __str__(self):
return str(self.user)
我也想要:
- 为每个用户创建具有 default/null 值的用户配置文件
(用户可以稍后更新)
- 用户可以输入电话
编号以及其他注册详情。
以下是我的URLs.py:
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^accounts/', include('registration.backends.hmac.urls')),
url(r'^', include('app.urls'))
]
由于我看不到任何 Django 注册表单,而且很多文档都描述了自定义用户模型(我没有),我有什么想法可以执行上述操作吗?
有很多不同的方法可以完成这个任务,最像 django 的方法是依赖信号。更具体地说,user_registered 信号。
registration.signals.user_registered Sent when a new user account is
registered. Provides the following arguments:
sender The RegistrationView subclass used to register the account.
user A user-model instance representing the new account.
request The
HttpRequest in which the new account was registered.
def create_user_profile(sender, user, requet):
'''
Creates a profile object for registered users via the
user_registered signal
'''
obj = UserProfile.objects.get_or_create(user=user)
另一种方法是子类化 RegistrationView。第二种选择是在 User 上捕获 post_save 信号。
我一直在努力实现不同的答案来处理(例如 Django-Registration & Django-Profile, using your own custom form),但未能在我的项目中使用它,因为它似乎太过时了。
基本上我已经在我的项目中安装了 Django-Registration 并且可以通过它验证和创建用户。
但是,我使用以下 UserProfile 模型扩展了用户:
class UserProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
account_type = models.IntegerField(null = True, default= 1)
daily_message = models.BooleanField(default = True)
tel_number = models.CharField(max_length=20, null = True)
def __str__(self):
return str(self.user)
我也想要:
- 为每个用户创建具有 default/null 值的用户配置文件 (用户可以稍后更新)
- 用户可以输入电话 编号以及其他注册详情。
以下是我的URLs.py:
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^accounts/', include('registration.backends.hmac.urls')),
url(r'^', include('app.urls'))
]
由于我看不到任何 Django 注册表单,而且很多文档都描述了自定义用户模型(我没有),我有什么想法可以执行上述操作吗?
有很多不同的方法可以完成这个任务,最像 django 的方法是依赖信号。更具体地说,user_registered 信号。
registration.signals.user_registered Sent when a new user account is registered. Provides the following arguments:
sender The RegistrationView subclass used to register the account.
user A user-model instance representing the new account.
request The HttpRequest in which the new account was registered.
def create_user_profile(sender, user, requet):
'''
Creates a profile object for registered users via the
user_registered signal
'''
obj = UserProfile.objects.get_or_create(user=user)
另一种方法是子类化 RegistrationView。第二种选择是在 User 上捕获 post_save 信号。