7

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

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

2 Answers2

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.

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

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:

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

That should do the trick! Good luck.

wunki
  • 653
  • 6
  • 13
  • Yeah I got your email. Thank you. Will you please check this linK? I think [there is bug]http://stackoverflow.com/questions/8818435/many-to-many-field-added-to-the-profile-does-not-work-django-userena) –  Jan 16 '12 at 18:40
  • 2
    Awesome! Now all you have to do is update the docs :) http://docs.django-userena.org/en/latest/faq.html#how-do-i-add-extra-fields-to-forms – Régis B. Feb 26 '13 at 11:53
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).

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.

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

-g

Greg
  • 6,571
  • 2
  • 27
  • 39