【问题标题】:adding extra fields to django-userena forms向 django-userena 表单添加额外的字段
【发布时间】:2012-01-15 06:47:47
【问题描述】:

我正在使用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 模型上——一个绑定到用户的实例

【问题讨论】:

  • 你用form的clean方法试过了吗?

标签: python django registration


【解决方案1】:

这里是 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}),

这应该可以解决问题!祝你好运。

【讨论】:

【解决方案2】:

我已经尝试了上述技巧,它似乎确实有效。但是,我遇到了一个错误,并且在我的安装中从未使用过保存功能。我的设置文件中有这个设置: USERENA_WITHOUT_USERNAMES=真 因此,当我扩充表单以包含新字段时,我永远不会使用 save 方法,因为我得到“必填字段”,这是针对我不使用的字段(用户名)。

我在视图创建中看到这里的行: 如果 userena_settings.USERENA_WITHOUT_USERNAMES 和 (signup_form == SignupForm): signup_form = SignupFormOnlyEmail 因此,我将示例更改为子类 SignupFormOnlyEmail 而不是 SignupForm 并且它可以工作。因此,如果您使用的是 USERENA_WITHOUT_USERNAMES,如果您想更改注册表单,请子类化不同的表单。

我意识到这并不能(确切地)回答这个问题,但是,它有点:-)

-g

【讨论】:

    猜你喜欢
    • 2018-10-10
    • 2019-12-30
    • 2018-01-19
    • 2020-01-04
    • 2011-07-04
    • 2021-09-29
    • 1970-01-01
    • 2012-02-04
    • 1970-01-01
    相关资源
    最近更新 更多