2

Based on Django's recommendation that information should be stored in a separate model if it's not directly related to authentication, I've created both a custom user model and a profile model in my app.

Something like:

class User(AbstractBaseUser):
    email = models.EmailField(
            verbose_name='email address',
            max_length=255,
            unique=True
            )
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    location = models.ForeignKey(Location)
    date_of_birth = models.DateField()
    date_joined = models.DateTimeField(auto_now_add=True)

    is_active = models.BooleanField(default=True)
    is_admin = models.BooleanField(default=False)

    objects = UserManager()

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['first_name', 'last_name', 'location', 'date_of_birth']

class Profile(models.Model):
    user = models.OneToOneField(User)

    picture = models.ImageField(upload_to='profile_pictures/', 
                              default='default.jpg')
    bio = models.TextField(blank=True)
    sex = models.CharField(max_length=10,
                           choices=(('Male', 'Male'),
                                    ('Female', 'Female'),
                                    ('No Comment', 'No Comment')),
                           default="No Comment")
    occupation = models.CharField(max_length=100, blank=True)

What's the best practice for having other models refer to a user? For example, my app has a messaging system. In the Message model, is it best to have a foreignkey relation to Profile as opposed to User? Should the User model only be used for the purpose of authentication?

Ben
  • 6,986
  • 6
  • 44
  • 71

1 Answers1

2

I think you can relate the models to User and use related name to access profile.

This makes your models not depend directly on custom profile.

Sai Prasanna
  • 684
  • 1
  • 10
  • 25