向django-userena表单添加额外的字段
我正在使用django-userena
。我有一个叫做UserProfile
的模型。我在注册表单中添加了额外的字段。这些字段显示正确,但数据未保存。我想将一些字段数据保存到另一个模型(Business
)中。例如,我有两个字段,如contact
和business
。我想联系领域将去UserProfile
模型和business
字段将去Business Model
。任何线索?谢谢向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.
"""
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
UPDATE:我上存储用户实例这些价值观......我想脚趾将它们存储在剖面模型 - 这是用户绑定一个实例
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}),
这应该做的伎俩!祝你好运。
是的,我收到了你的电子邮件。谢谢。你能检查一下这个LINK吗?我认为[有bug] http://stackoverflow.com/questions/8818435/many-to-many-field-added-to-the-profile-does-not-work-django-userena) – Kulbir 2012-01-16 18:40:39
太棒了!现在你所要做的就是更新文档:) http://docs.django-userena.org/en/latest/faq.html#how-do-i-add-extra-fields-to-forms – 2013-02-26 11:53:40
我已经尝试了上述技巧,它似乎工作。但是,我遇到了一个错误,并且从未进入安装过程中的保存功能。我在我的设置文件中有这样的设置: USERENA_WITHOUT_USERNAMES = True 因此,当我扩充表单以包含新字段时,我从来没有进入保存方法,因为我得到'字段必需',这是我不是使用(用户名)。
我在视图创建线路在这里看到: 如果userena_settings.USERENA_WITHOUT_USERNAMES和(signup_form == SignupForm): signup_form = SignupFormOnlyEmail 于是,我改变了例子子类SignupFormOnlyEmail而不是SignupForm和它的作品。因此,如果您正在使用USERENA_WITHOUT_USERNAMES,那么如果您想更改注册表单,则可以使用其他表单子类。
我知道这不回答这个问题(准确),但是,它确实有点:-)
-g
你尝试过它在形式上的清洁方法? – Ahsan 2012-01-15 15:52:19