Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 向django userena表单添加额外字段_Python_Django_Registration - Fatal编程技术网

Python 向django userena表单添加额外字段

Python 向django userena表单添加额外字段,python,django,registration,Python,Django,Registration,我使用的是django userena。我有一个名为UserProfile的模型。我在注册表单中添加了额外的字段。这些字段显示正确,但数据未保存。我还想将一些字段数据保存到另一个模型(Business)中。例如,我有两个字段,如联系人和业务。“我想要联系人”字段将转到UserProfileModel,而business字段将转到business Model。有线索吗?多谢各位 这是我的密码 class SignupFormExtra(SignupForm): address = form

我使用的是
django userena
。我有一个名为
UserProfile
的模型。我在注册表单中添加了额外的字段。这些字段显示正确,但数据未保存。我还想将一些字段数据保存到另一个模型(
Business
)中。例如,我有两个字段,如
联系人
业务
。“我想要联系人”字段将转到
UserProfile
Model,而
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模型上——一个绑定到Userena的用户的实例。我已经收到了一封“无法访问”的电子邮件,但如果其他人也有同样的问题,那么值得指出解决方案。第一个错误是
save
方法返回一个概要文件。这不是真的,它返回一个Django
用户
。因此,您首先必须获取概要文件并对其进行更改。保存配置文件,然后再次返回用户以使其与Userena兼容

对于
业务
模型,只需将其添加到
保存
方法中即可

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

那就应该成功了!祝你好运。

我已经尝试了上述技巧,它似乎确实有效。但是,我遇到了一个错误,在我的安装中从未使用过save函数。我的设置文件中有此设置: USERENA_不带_用户名=True 因此,当我扩充表单以包含新字段时,我永远不会使用save方法,因为我得到的是“fieldrequired”,这是我没有使用的字段(username)

我在视图创建中看到以下行: 如果userena_settings.userena_没有用户名和(signup_form==SignupForm): signup\u form=signupformonlymail 因此,我将示例更改为将SignupFormOnlyMail子类化,而不是将SignupForm子类化,它可以正常工作。因此,如果您使用的是没有用户名的USERENA_,那么如果您想更改注册表单,可以将其子类化为另一个表单

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


-g

你试过用form的干净方法吗?是的,我收到了你的电子邮件。非常感谢。请检查一下这个链接好吗?我觉得[有虫子])太棒了!现在,您只需更新文档:)