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

时间:2021-04-01 15:47:59

I am using django-userena. I have a model called UserProfile. I've added extra fields in signup form. and These fields are show up correctly but data is not saved. I want to save some fields data into another Model (Business) too. For example I've two field like contact and business. I want contact field will goes to UserProfile Model and business field will goes to Business Model. any clue? Thank you

我正在使用django-userena。我有一个名为UserProfile的模型。我在注册表单中添加了额外的字段。和这些字段显示正确但不保存数据。我想将一些字段数据保存到另一个模型(业务)中。例如,我有两个领域,如联系和业务。我想联系字段将转到UserProfile模型,业务领域将转到业务模型。任何线索?谢谢

Here is my code

这是我的代码

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 : I am storing those values on a User instance... I want toe storing them on Profile model -- an instance that's bound to the User

更新:我将这些值存储在User实例上...我想将它们存储在Profile模型上 - 一个绑定到User的实例

2 个解决方案

#1


10  

Author of Userena here. I already had an e-mail correspondence with "no_access", but it's worth pointing to the solution if others have the same problem. The first mistake was that the save method returns a profile. This is not true, it returns a Django User. Because of this you first have to fetch the profile and make the changes to it. Save the profile and then return the user again to keep it compatible with Userena.

Userena的作者在这里。我已经收到了与“no_access”的电子邮件通信,但如果其他人遇到同样的问题,则值得指出解决方案。第一个错误是save方法返回一个配置文件。这不是真的,它返回一个Django用户。因此,您首先必须获取配置文件并对其进行更改。保存配置文件,然后再次返回用户以使其与Userena兼容。

For the Business model, just add it in the save method also.

对于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

After creating the form, don't forget to override the userena form in your urls.py. Something like this will do:

创建表单后,不要忘记覆盖urls.py中的userena表单。这样的事情会做:

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

That should do the trick! Good luck.

这应该够了吧!祝你好运。

#2


1  

I have tried the above trick, and it does seem to work. However, I am running in to an error and never getting to the save function in my installation. I have this setting in my settings file: USERENA_WITHOUT_USERNAMES=True So, when I augment the form to include new fields, I never get to the save method because I get 'field required', this is for the field that I am not using (username).

我尝试了上面的技巧,它似乎确实有效。但是,我遇到了一个错误,并且从未进入我的安装中的保存功能。我在我的设置文件中有这个设置:USERENA_WITHOUT_USERNAMES = True因此,当我扩充表单以包含新字段时,我从未进入保存方法,因为我得到'字段必需',这是我不使用的字段(用户名)。

I see in the view creation the line here: if userena_settings.USERENA_WITHOUT_USERNAMES and (signup_form == SignupForm): signup_form = SignupFormOnlyEmail So, I changed the example to subclass the SignupFormOnlyEmail instead of the SignupForm and it works. So, if you are using the USERENA_WITHOUT_USERNAMES, subclass a different form if you want to change the signup form.

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

I realize this doesn't answer this question (exactly), but, it kinda does :-)

我意识到这并没有回答这个问题(确切地说),但是,有点确实如此:-)

-g

-G

#1


10  

Author of Userena here. I already had an e-mail correspondence with "no_access", but it's worth pointing to the solution if others have the same problem. The first mistake was that the save method returns a profile. This is not true, it returns a Django User. Because of this you first have to fetch the profile and make the changes to it. Save the profile and then return the user again to keep it compatible with Userena.

Userena的作者在这里。我已经收到了与“no_access”的电子邮件通信,但如果其他人遇到同样的问题,则值得指出解决方案。第一个错误是save方法返回一个配置文件。这不是真的,它返回一个Django用户。因此,您首先必须获取配置文件并对其进行更改。保存配置文件,然后再次返回用户以使其与Userena兼容。

For the Business model, just add it in the save method also.

对于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

After creating the form, don't forget to override the userena form in your urls.py. Something like this will do:

创建表单后,不要忘记覆盖urls.py中的userena表单。这样的事情会做:

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

That should do the trick! Good luck.

这应该够了吧!祝你好运。

#2


1  

I have tried the above trick, and it does seem to work. However, I am running in to an error and never getting to the save function in my installation. I have this setting in my settings file: USERENA_WITHOUT_USERNAMES=True So, when I augment the form to include new fields, I never get to the save method because I get 'field required', this is for the field that I am not using (username).

我尝试了上面的技巧,它似乎确实有效。但是,我遇到了一个错误,并且从未进入我的安装中的保存功能。我在我的设置文件中有这个设置:USERENA_WITHOUT_USERNAMES = True因此,当我扩充表单以包含新字段时,我从未进入保存方法,因为我得到'字段必需',这是我不使用的字段(用户名)。

I see in the view creation the line here: if userena_settings.USERENA_WITHOUT_USERNAMES and (signup_form == SignupForm): signup_form = SignupFormOnlyEmail So, I changed the example to subclass the SignupFormOnlyEmail instead of the SignupForm and it works. So, if you are using the USERENA_WITHOUT_USERNAMES, subclass a different form if you want to change the signup form.

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

I realize this doesn't answer this question (exactly), but, it kinda does :-)

我意识到这并没有回答这个问题(确切地说),但是,有点确实如此:-)

-g

-G