I have read a lot about how to add an extra field when using Django-registration, for example here, here and here. The code snippets are: forms.py (from the registration app)
class RegistrationForm(forms.Form):
username = forms.RegexField(regex=r'^\w+$', max_length=30, widget=forms.TextInput(attrs=attrs_dict), label=_("Username"), error_messages={ 'invalid': _("This value must contain only letters, numbers and underscores.") })
email = forms.EmailField(widget=forms.TextInput(attrs=dict(attrs_dict, maxlength=75)), label=_("Email"))
password1=forms.CharField(widget=forms.PasswordInput(attrs=attrs_dict, render_value=False), label=_("Password"))
institute=forms.CharField(max_length=50) #This is the new!
password2 = forms.CharField(widget=forms.PasswordInput(attrs=attrs_dict, render_value=False), label=_("Password (again)"))
captcha = CaptchaField()
I use the django's registration save method, which is in the default backend:
def register(self, request, **kwargs):
username, email, password = kwargs['username'], kwargs['email'], kwargs['password1']
if Site._meta.installed:
site = Site.objects.get_current()
else:
site = RequestSite(request)
new_user = RegistrationProfile.objects.create_inactive_user(username, email,
password, site)
signals.user_registered.send(sender=self.__class__,
user=new_user,
request=request)
return new_user
In models.py I have created a new class:
class UserProfile(models.Model):
user = models.OneToOneField(User)
institute=models.CharField(max_length=50)
def create_user_profile(sender, instance, created, **kwargs):
if created:
profile, created = UserProfile.objects.get_or_create(user=instance)
post_save.connect(create_user_profile, sender=User)
In the admin.py:
admin.site.unregister(User)
class UserProfileInline(admin.StackedInline):
model = UserProfile
class UserProfileAdmin(UserAdmin):
inlines = [ UserProfileInline, ]
admin.site.register(User, UserProfileAdmin)
I have not altered anything alse. When I render the form I can see the Institute Field I have added. When I am in the admin site I can see the user profile of each user. However nothing is saved there when I create a new user while the other details of the user are normally saved. I think that I need some kind of configuration but where? Maybe in the files of the Django-registration app?