Django-Userena:为用户的个人资料添加额外的非空字段

时间:2020-12-11 02:32:51

Is there an easy way to allow for required profile fields?

是否有一种简单的方法来允许所需的配置文件字段?

I am using userena in my current django project. I have a custom profile called UserProfile which has a start_year none-blank, non-null field as defined below.

我在我目前的django项目中使用userena。我有一个名为UserProfile的自定义配置文件,它具有start_year非空,非空字段,如下所述。

class UserProfile(UserenaBaseProfile, PybbProfile):
    user = models.OneToOneField(User, unique=True,
                        verbose_name=_('user'),
                        related_name='user_profile')

    start_year = models.IntegerField(max_length=4)

I need this to be filled-in on signup. I created a SignupExtraForm as defined below, to override the default form.

我需要在注册时加入。我创建了一个如下定义的SignupExtraForm来覆盖默认表单。

class SignupFormExtra(SignupForm):
    start_year = forms.IntegerField(label=_(u'Initiation Year'),
                            min_value=1800,
                            max_value=datetime.now().year,
                            required=True)

    def save(self):
        new_user = super(SignupFormExtra, self).save()

        new_user_profile = new_user.get_profile()

        new_user_profile.start_year = self.cleaned_data['start_year']
        new_user_profile.save()

        # Userena expects to get the new user from this form, so return the new
        # user.
        return new_user

When I attempt to add a new user thru the now modified form I get the below error:

当我尝试通过现在修改的表单添加新用户时,我得到以下错误:

profile_userprofile.start_year may not be NULL

With the stack trace pointing at new_user = super(SignupFormExtra, self).save()), in the code above.

堆栈跟踪指向new_user = super(SignupFormExtra,self).save()),在上面的代码中。

I think this has to do with the user profile being created and saved before I am able to give it the required data from the form. Is there an easy way of supplying this data to the user_creation process, or delaying the creating of the user profile?

我认为这与我在能够从表单中提供所需数据之前创建和保存的用户配置文件有关。有没有一种简单的方法可以将这些数据提供给user_creation进程,或者延迟创建用户配置文件?

Thanks Shon

谢谢Shon

1 个解决方案

#1


0  

UserProfile is created after the User is saved by a post_save signal. Even if you override it with your own signal, you won't have access to the form data from there.

UserProfile是在post_save信号保存用户后创建的。即使您使用自己的信号覆盖它,您也无法从那里访问表单数据。

The easiest solution is to just allow start_year to be NULL. It's not necessary to enforce this at the database level, and you can make the field required in all forms either way:

最简单的解决方案是让start_year为NULL。没有必要在数据库级别强制执行此操作,并且您可以以任何方式使所有表单都需要该字段:

start_year = models.IntegerField(max_length=4, blank=False, null=True)

Your custom form already enforces that the field is required, so you're done.

您的自定义表单已经强制要求该字段,所以您已完成。

UPDATE (from comment)

更新(来自评论)

Custom form, yes, but you can still use a ModelForm:

自定义表单,是的,但您仍然可以使用ModelForm:

class MyModelForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(MyModelForm, self).__init__(*args, **kwargs)
        self.fields['start_year'].required = True

UPDATE (again)

更新(再次)

Actually, I didn't think before my last update. I put blank=False on the start_year field in my example. That will force that the field is required in all ModelForms by default. You don't need a custom ModelForm at all. However, I left the previous update for posterity.

实际上,在我上次更新之前,我没有想到。我在示例中的start_year字段中输入了blank = False。这将强制默认情况下所有ModelForms中都需要该字段。您根本不需要自定义ModelForm。但是,我为后代留下了上一次更新。

#1


0  

UserProfile is created after the User is saved by a post_save signal. Even if you override it with your own signal, you won't have access to the form data from there.

UserProfile是在post_save信号保存用户后创建的。即使您使用自己的信号覆盖它,您也无法从那里访问表单数据。

The easiest solution is to just allow start_year to be NULL. It's not necessary to enforce this at the database level, and you can make the field required in all forms either way:

最简单的解决方案是让start_year为NULL。没有必要在数据库级别强制执行此操作,并且您可以以任何方式使所有表单都需要该字段:

start_year = models.IntegerField(max_length=4, blank=False, null=True)

Your custom form already enforces that the field is required, so you're done.

您的自定义表单已经强制要求该字段,所以您已完成。

UPDATE (from comment)

更新(来自评论)

Custom form, yes, but you can still use a ModelForm:

自定义表单,是的,但您仍然可以使用ModelForm:

class MyModelForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(MyModelForm, self).__init__(*args, **kwargs)
        self.fields['start_year'].required = True

UPDATE (again)

更新(再次)

Actually, I didn't think before my last update. I put blank=False on the start_year field in my example. That will force that the field is required in all ModelForms by default. You don't need a custom ModelForm at all. However, I left the previous update for posterity.

实际上,在我上次更新之前,我没有想到。我在示例中的start_year字段中输入了blank = False。这将强制默认情况下所有ModelForms中都需要该字段。您根本不需要自定义ModelForm。但是,我为后代留下了上一次更新。