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
外部キー関係を持つのが最善ですか? モデルは認証の目的でのみ使用する必要がありますか?Profile
User
User