django-registration app and Django 1.5 custom user model
Asked Answered
F

3

11

I use django-registration app and Django 1.5. How to create (new in django) custom user model and save also this data during registration (Please note that I am using django-registration):

class CustomProfile(models.Model):
    user = models.ForeignKey(User)
    name = models.CharField(max_length=255)
    bank = models.CharField(max_length=255)
    address = models.CharField(max_length=255)

?

Fillip answered 8/3, 2013 at 14:29 Comment(2)
Did you see this? #1072770Bugle
@Bugle I try with new custom user model (new in Django 1.5)Fillip
M
9

django-registration's main fork is not compatible with django 1.5 for now.

Check this pull request.

You have three options:

  • Patch django-registration's code. You can get the changes needed from the pull request.
  • Use an unofficial fork that is already patched. This one for example.
  • Wait for an update on the main fork...
Melo answered 8/3, 2013 at 15:51 Comment(2)
I just downloaded the current "trunk" version of the django-registration app from bitbucket, and it works with Django 1.5 (it did not work when I used pip to install the app). The version number I downloaded is VERSION = (0, 9, 0, 'beta', 1).Burrus
maybe django-registration2 ? I've just installed it instead django-registration because yolk -V django-registration gives only django-registration 0.8 which is not compatible with Django==1.5.1Heywood
B
4

This link explains the process well and works with django-registration 1.0

here are a few extra pointers in addition to the above code.

To update the first name change this in the models.py

def user_registered_callback(sender, user, request, **kwargs):
profile = ExUserProfile(user = user)
profile.is_human = bool(request.POST["is_human"])
user.first_name = request.POST["firstname"]
user.save()
profile.save()

user_registered.connect(user_registered_callback)

and in the forms.py file

class ExRegistrationForm(RegistrationForm):
    is_human = forms.BooleanField(label = "Are you human?:")
    firstname = forms.CharField(max_length=30)
    lastname = forms.CharField(max_length=30)

finally to see the changes on the form create an appropriate template. The profile can be seen in the admin by creating a file called admin.py in your app and write the following code

from django.contrib import admin
from django.contrib.auth.models import User
from django.contrib.auth.admin import UserAdmin
from prof.models import ExUserProfile

admin.site.unregister(User)

class UserProfileInline(admin.StackedInline):
    model = ExUserProfile

class UserProfileAdmin(UserAdmin):
    inlines = [ UserProfileInline, ]

admin.site.register(User, UserProfileAdmin)
Built answered 2/7, 2013 at 5:42 Comment(2)
Won't this actually create both a User and ExUserProfile instance?Flexuous
Yes, that is objective the custom user model is created by associating the ExUserProfile to the user object and saving the necessary fieldsBuilt
U
0

Django-registration 1.0 has been released recently. It was completely rewritten to use class based views and with Django 1.0 custom user model support. Check out the docs.

Unchaste answered 23/7, 2013 at 22:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.