2
Django의 권장 사항 인 information should be stored in a separate model if it's not directly related to authentication에 기반하여 내 응용 프로그램에 사용자 정의 사용자 모델과 프로필 모델을 모두 작성했습니다. 다른 모델은 사용자에게 참조하게하기위한 가장 좋은 방법은 무엇Django 모범 사례 - 사용자 정의 사용자 또는 프로파일 모델 참조
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)
: 같은
는뭔가? 예를 들어 내 앱에는 메시징 시스템이 있습니다. Message
모델에서 User
이 아닌 Profile
과 외래 키 관계를 갖는 것이 가장 좋습니다. 인증 목적으로 만 User
모델을 사용해야합니까?
예, 사용자는 항상 사용할 수 있으며 프로필의 존재를 확인하고 그에 따라 행동 할 수 있습니다. –