在django-userena表单中添加额外字段

7 投票
2 回答
2426 浏览
提问于 2025-04-17 10:16

我正在使用 django-userena。我有一个叫 UserProfile 的模型。我在注册表单中添加了一些额外的字段,这些字段显示得很好,但数据却没有保存。我想把一些字段的数据也保存到另一个模型 Business 中。例如,我有两个字段,一个是 contact(联系信息),另一个是 business(业务信息)。我希望 contact 字段的数据保存到 UserProfile 模型中,而 business 字段的数据保存到 Business 模型中。有没有什么建议?谢谢

这是我的代码

class SignupFormExtra(SignupForm):
    address = forms.CharField(label=_(u'Address'),max_length=30,required=False)
    contact = forms.CharField(label=_(u'Contact'),max_length=30,required=False)
    business = forms.CharField(label=_(u'Business Name'),max_length=30,required=False)

    def save(self):
        """
        Override the save method to save the first and last name to the user
        field.

        """

        user_profile = super(SignupFormExtra, self).save(commit=False)

        user_profile.address = self.cleaned_data['address']
        user_profile.contact = self.cleaned_data['contact']
        user_profile.business = self.cleaned_data['business']

        user_profile.save()

        return user_profile

更新:我现在把这些值存储在一个用户实例上……我想把它们存储在与用户绑定的 Profile 模型中。

2 个回答

1

我试过上面的方法,确实有效。不过,我遇到了一个错误,安装过程中从来没有进入保存功能。我在设置文件里有这个选项:USERENA_WITHOUT_USERNAMES=True。所以,当我在表单中添加新字段时,始终无法进入保存方法,因为我收到“字段必填”的提示,这个提示是针对我没有使用的字段(用户名)。

我在创建视图时看到这里有一行代码:
if userena_settings.USERENA_WITHOUT_USERNAMES and (signup_form == SignupForm):
signup_form = SignupFormOnlyEmail
所以,我把示例改成了继承SignupFormOnlyEmail而不是SignupForm,这样就可以正常工作了。因此,如果你使用USERENA_WITHOUT_USERNAMES,想要更改注册表单,就要继承一个不同的表单。

我知道这并没有完全回答这个问题,但也算是有点关系吧 :-)

-g

10

这里是Userena的作者。我之前和“no_access”有过邮件交流,但如果其他人也遇到同样的问题,值得分享一下解决方案。第一个错误是,save方法返回的是一个个人资料。这其实不对,它返回的是一个Django的User对象。因此,你首先需要获取个人资料,然后对其进行修改。修改完个人资料后再保存,然后再返回用户,以保持与Userena的兼容性。

对于Business模型,也要在save方法中添加它。

class SignupFormExtra(SignupForm):
    address = forms.CharField(label=_(u'Address'),max_length=30,required=False)
    contact = forms.CharField(label=_(u'Contact'),max_length=30,required=False)
    business = forms.CharField(label=_(u'Business Name'),max_length=30,required=False)

    def save(self):
        """
        Override the save method to save the first and last name to the user
        field.

        """
        # Original save method returns the user
        user = super(SignupFormExtra, self).save()

        # Get the profile, the `save` method above creates a profile for each
        # user because it calls the manager method `create_user`.
        # See: https://github.com/bread-and-pepper/django-userena/blob/master/userena/managers.py#L65
        user_profile = user.get_profile()

        # Be sure that you have validated these fields with `clean_` methods.
        # Garbage in, garbage out.
        user_profile.address = self.cleaned_data['address']
        user_profile.contact = self.cleaned_data['contact']
        user_profile.save()

        # Business
        business = self.cleaned_data['business']
        business = Business.objects.get_or_create(name=business)
        business.save()

        # Return the user, not the profile!
        return user

创建表单后,别忘了在你的urls.py文件中覆盖userena的表单。像这样就可以了:

url(r'^accounts/signup/$',
        userena_views.signup,
        {'signup_form': SignupFormExtra}),

这样就可以解决问题了!祝你好运。

撰写回答