tags:

views:

106

answers:

3

I am using the create_user() function that Django provides to create my users. Also I want to store additional information about the users. So I tried following the instructions given at

http://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users

but I cannot get it to work for me. Is there a step-by-step guide that I can follow to get this to work for me?

Also, once I have added these custom fields, I would obviously need to add / edit / delete data from them. I cannot seem to find any instructions on how to do this.

A: 

It's just another model. You manipulate it exactly the same way you manipulate every other model you come across.

Ignacio Vazquez-Abrams
+4  A: 

I am not aware of a step by step(though I am sure a solid google would produce something). But here is a quick go at it.

1) Create a UserProfile model to hold the extra information and put it in your models.py. It could look something like this:

class UserProfile(models.Model):
    #required by the auth model
    user = models.ForeignKey(User, unique=True)
    middle_name = models.CharField(max_length=30, null=True, blank=True)

2) Tell your settings.py about the new class by adding this line (with the appropriate name):

AUTH_PROFILE_MODULE = "myapp.UserProfile"

3) Add a signal listener to create a blank UserProfile record when a new user is added. You can find a great snippet with directions here.

4) When processing the new user record you can populate the UserProfile record as well. Here is how I do the insert (notice the get_profile):

if (form.is_valid()):
    cd = form.cleaned_data
    user = User.objects.create_user(cd["UserName"], cd["Email"], cd["Password"])
    user.first_name = cd["FirstName"]
    user.last_name = cd["LastName"]
    user.save()
    #Save userinfo record
    uinfo = user.get_profile()
    uinfo.middle_name = cd["MiddleName"]
    uinfo.save()

That is all there is to it. This is not comprehensive, but should point you in the right direction.

Jason Webb
I've tried that. I get and error stating 'NoneType' object has no attribute '_default_manager'.
Gaurav
Tried what specifically? Where are you getting that error?
Jason Webb
Created a file named UserProfile.py. Added the required code. Made changes in settings.py(AUTH_PROFILE_MODULE = 'app.UserProfile').I get an error while trying to save the middle name i.e. at the line uinfo = user.get_profile()
Gaurav
You need to put the UserProfile in models.py. Then run `python manage.py syncdb` to apply to your database. Check your database to be sure that the table was actually created. Then try again.
Jason Webb
A: 

@Gaurav I think you have to make UserProfile "model" class in models.py and then go on as mentioned above by BigJason.

zubinmehta