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
型號是否僅用於驗證目的?
是的,用戶將始終可用,然後您可以檢查配置文件的存在並相應地採取行動 – 2014-10-08 10:15:41