Adding custom fields to users in django


Question

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.

1
25
5/22/2010 5:18:26 AM

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
22
12/23/2015 1:26:44 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon