0

I'm using Django Registration(https://bitbucket.org/ubernostrum/django-registration/), and I need to add some fields to the registration of users.

I've made a subclass of the RegistrationForm. My "forms.py" is the following:

from django.contrib.auth.models import User
from myproject.apps.userprofile.models import Gender, UserProfile
from myproject.apps.location.models import Country, City
from django import forms
from registration.forms import RegistrationForm
from registration.models import RegistrationManager


class RegistrationFormExtend(RegistrationForm):
    """
    Subclass of ``RegistrationForm`` which adds the fiedls in the userprofile app
    """   
    gender              = forms.ModelChoiceField(queryset=Gender.objects.all(), empty_label="(Nothing)")
    country             = forms.ModelChoiceField(queryset=Country.objects.all(), empty_label="(Nothing)")
    city                = forms.ModelChoiceField(queryset=City.objects.all(), empty_label="(Nothing)")
    #profile_picture     = 

To make it work I've changed the "urls.py" to show the "RegistrationFormExtend" form by adding the "form_class" parameter to the "register" view:

from django.conf.urls.defaults import *
from django.views.generic.simple import direct_to_template

from registration.views import activate
from registration.views import register

from myproject.apps.registrationextend.forms import RegistrationFormExtend


urlpatterns = patterns('',
               ...
                   url(r'^registar/$',
                       register,
                       {'backend': 'registration.backends.default.DefaultBackend', 'form_class': RegistrationFormExtend,},
                       name='registration_register'),
                   ...
                   )

After that, I've tested and the form is working. The user is registered successfully, but all the extra fields(gender, country, city) in the "RegistrationFormExtend" are NOT stored in the database.

Reading the documentation, http://docs.b-list.org/django-registration/0.8/views.html#registration.views.register it seems that I must to pass the parameter "extra_context" to the view.

My question is how to pass a dictionary to the "extra_context" parameter. How to refer to the variables "gender", "country" and "city"?

Thanks in advance.

André
  • 24,706
  • 43
  • 121
  • 178

1 Answers1

0

You forgot to have save method in your extended registration form. You have to do something like this:

class RegistrationFormExtend(RegistrationForm):
    """
    Subclass of ``RegistrationForm`` which adds the fiedls in the userprofile app
    """   
    gender = forms.ModelChoiceField(queryset=Gender.objects.all(), empty_label="(Nothing)")
    country = forms.ModelChoiceField(queryset=Country.objects.all(), empty_label="(Nothing)")
    city = forms.ModelChoiceField(queryset=City.objects.all(), empty_label="(Nothing)")
    #profile_picture

    def __init__(self, *args, **kw):
       super(RegistrationFormExtend, self).__init__(*args, **kw)

    def save(self):
        #first save the parent form
        profile = super(RegistrationFormExtend, self).save()
        #then save the custom fields
        #i am assuming that the parent form return a profile
        profile.gender = self.cleaned_data['gender']
        profile.country = self.cleaned_data['country']
        profile.city = self.cleaned_data['city']
        profile.save()
        return profile
Aamir Rind
  • 38,793
  • 23
  • 126
  • 164
  • Hi Aamir Adnan! Thanks for your reply. I have tested the save method with a "pdb.set_trace()" inside. The code does not enter inside the save method. What calls this save method? How can I make the code enter inside the save method? Thanks again. – André Nov 17 '12 at 19:37
  • 1
    does gender, country and city fields are even rendered in template? – Aamir Rind Nov 17 '12 at 19:42
  • Yes, the are rendered. And I see the values passing. The problem is not in the form. I'm reading the Django documentation. Should I create a "def create_user_profile" as described here? https://docs.djangoproject.com/en/1.4/topics/auth/#storing-additional-information-about-users – André Nov 17 '12 at 19:51
  • 1
    May be this http://stackoverflow.com/questions/3114976/extending-django-registration-using-signals will help more and this http://dewful.com/?p=70 – Aamir Rind Nov 17 '12 at 20:00
  • Hi Aamir Adnan. I've tested you new solution, but is not working. I will read the links that you gave me. Many thanks for your help. – André Nov 17 '12 at 20:08