Django - Multiple User Profiles

前端 未结 3 461
小鲜肉
小鲜肉 2021-01-29 21:27

Initially, I started my UserProfile like this:

from django.db import models
from django.contrib.auth.models import User

class UserProfile(models.Model):
    use         


        
3条回答
  •  清歌不尽
    2021-01-29 21:57

    I have done it this way.

    PROFILE_TYPES = (
        (u'INDV', 'Individual'),
        (u'CORP', 'Corporate'),
    )
    
    # used just to define the relation between User and Profile
    class UserProfile(models.Model):
        user = models.ForeignKey(User)
        profile = models.ForeignKey('Profile')
        type = models.CharField(choices=PROFILE_TYPES, max_length=16)
    
    # common fields reside here
    class Profile(models.Model):
        verified = models.BooleanField(default=False)
    

    I ended up using an intermediate table to reflect the relation between two abstract models, User which is already defined in Django, and my Profile model. In case of having attributes that are not common, I will create a new model and relate it to Profile.

提交回复
热议问题