Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django user profile

When adding additional fields to a user profile, such as location, gender, employer, etc., should I be adding additional columns to django.contrib.auth.models.User and saving it there? Or should I be creating a new table to save user profile information?

Also, when a user uploads a profile picture, should I be saving this in the same table? (Note this is not a production server, I'm just doing this on my local runserver to figure things out). Thank you

like image 573
David542 Avatar asked May 21 '11 23:05

David542


2 Answers

You have to make a model for the user profile:

class UserProfile(models.Model):       user = models.ForeignKey(User, unique=True)     location = models.CharField(max_length=140)       gender = models.CharField(max_length=140)       employer = models.ForeignKey(Employer)     profile_picture = models.ImageField(upload_to='thumbpath', blank=True)      def __unicode__(self):         return u'Profile of user: %s' % self.user.username 

Then configure in settings.py:

AUTH_PROFILE_MODULE = 'accounts.UserProfile' 
like image 59
Ezequiel Marquez Avatar answered Sep 24 '22 22:09

Ezequiel Marquez


Conceptually, OneToOneField is similar to a ForeignKey with unique=True, but the “reverse” side of the relation will directly return a single object. This is the recommended way of extending User class.

class UserProfile(models.Model):       user = models.OneToOneField(User)     ... 
like image 34
Dmitry Avatar answered Sep 25 '22 22:09

Dmitry