31
votes

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.

4
so you are the Django create_user() function? :) - ax.
I thought I was but unfortunately I am not :( .... Just a regular old human..Thanks for the grammar correction ax. Any idea on how to solve my actual problem? - Gaurav Sharma

4 Answers

25
votes

The recommended way is to create a new model and give it a OneToOneField() with the built-in User model like so:

class Student(models.Model):
    user = models.OneToOneField(User)
    college = models.CharField(max_length=30)
    major = models.CharField(max_length=30)

etc.

Then you can access the fields like this:

user = User.objects.get(username='jsmith')
college = user.student.college
20
votes

I know It's too late and Django has changed a lot since then, but just for seekers,

According to Django documentation if you're happy with User model and just want to add some extra fields, do these:

1- Your models.py should be like this:

from django.contrib.auth.models import AbstractUser
class CustomUser(AbstractUser):
    mobile = models.CharField(max_length=16)
    # if your additional field is a required field, just add it, don't forget to add 'email' field too.
    # REQUIRED_FIELDS = ['mobile', 'email']

2- add this to the setting.py

AUTH_USER_MODEL = 'myapp.CustomUser'

done!

Now you can run python manage.py syncdb

19
votes

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.

Update: Please note that AUTH_PROFILE_MODULE is deprecated since v1.5: https://docs.djangoproject.com/en/stable/releases/1.5/#auth-profile-module

-9
votes

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