向Django-Userena表单中添加额外字段

7
我正在使用 django-userena。我有一个名为UserProfile的模型。我在注册表单中添加了额外的字段。这些字段正确显示,但数据没有保存。我想将一些字段数据保存到另一个模型(Business)中。例如,我有两个字段,如contactbusiness。我希望联系人字段将转到UserProfile模型,而business字段将转到Business Model。有什么线索吗?谢谢
以下是我的代码:
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模型上--一个绑定到用户的实例。


你在表单的清除方法中尝试过吗? - Ahsan
2个回答

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}),

应该就可以了!祝好运。


是的,我收到了你的邮件。谢谢。你能看一下这个链接吗?我觉得有一个错误。(http://stackoverflow.com/questions/8818435/many-to-many-field-added-to-the-profile-does-not-work-django-userena) - user514310
2
太棒了!现在你只需要更新文档 :) http://docs.django-userena.org/en/latest/faq.html#how-do-i-add-extra-fields-to-forms - Régis B.

1
我已经尝试了上述技巧,似乎确实有效。然而,我遇到了一个错误,从未进入我的安装中的保存函数。我在设置文件中有这个设置: USERENA_WITHOUT_USERNAMES=True 因此,当我增加表单以包括新字段时,我永远不会到达保存方法,因为我得到“字段必填”,这是我没有使用的字段(用户名)。
我在视图创建中看到这一行: 如果userena_settings.USERENA_WITHOUT_USERNAMES和(signup_form == SignupForm), 则signup_form = SignupFormOnlyEmail 所以,我改变了示例,继承了SignupFormOnlyEmail而不是SignupForm,它起作用了。因此,如果您正在使用USERENA_WITHOUT_USERNAMES,请继承一个不同的表单,如果要更改注册表单。
我意识到这并没有完全回答这个问题,但是,它有点回答了 :-)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接