基于 Django 的建议 information should be stored in a separate model if it's not directly related to authentication ,我在我的应用程序中创建了自定义用户模型和配置文件模型。
类似于:
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 模型中,与 Profile 而非 User 的外键关系是否最好? User 模型是否应该仅用于身份验证?
请您参考如下方法:
我认为您可以将模型与用户相关联并使用相关名称来访问个人资料。
这使您的模型不直接依赖于自定义配置文件。
