使用RegistrationView时填充UserProfile

时间:2015-12-04 18:26:29

标签: django django-registration

我有一个自定义UserProfile:

class UserProfile(models.Model):
    BUYER_USER = 1
    CLIENT_USER = 2
    ARTIST_USER = 3
    ADMIN_USER = 3
    USER_CHOICES = (
        (BUYER_USER, 'Buyer'),
        (CLIENT_USER, 'Client'),
        (ARTIST_USER, 'Artist'),
        (ADMIN_USER, 'Admin'),
    )

    # This field is required.
    user = models.OneToOneField(User)

    usertype = models.IntegerField(choices=USER_CHOICES, default=BUYER_USER)
    dob = models.DateField(blank=True, null=True)
    address = models.TextField(max_length=8, blank=True, null=True)

我正在使用django-registration-redux来管理帐户。内置注册视图具有以下布局:

enter image description here

但我想转到以下布局:

enter image description here

我知道如何更改模板并调整与User模型相关的字段的布局,但我不知道如何告诉django-registration-redux填充UserProfile基于以RegistrationView形式输入的数据。

这一切都可能吗?怎么样?

1 个答案:

答案 0 :(得分:1)

我认为可能的答案是为UserProfile创建一个表单,将其添加到redux注册视图。找到视图函数并查看他们检查表单的位置是否正在使用is_valid()提交,并将您的Userform添加到if块,以检查它是否也有效。为了能够使用redux注册表单填充UserProfile,您可以执行以下操作

user_form.save(commit=false)
user_form.user = registration_form.user
user_form.save()

通过这种方式,UserProfile用户字段将填充注册模型中的user字段。

道歉,如果这是完全错误的,因为我不熟悉registration-redux包,但这就是我通常这样做的方式。

以下是关于如何使用基于类的视图执行此操作的github要点:https://gist.github.com/michelts/1029336

在这种情况下,您需要修改registration-redux registration/views.py https://github.com/macropin/django-registration/blob/master/registration/views.py#L68-L119