我有以下两种型号:
class CustomUser(AbstractUser):
objects = CustomUserManager()
class Meta:
ordering = ['nin']
verbose_name = 'User'
verbose_name_plural = 'Users'
username = None
name = models.CharField(max_length=50)
nin = models.CharField(verbose_name= 'National Insurance Number', max_length=9, unique=True, null=False, blank=False, validators=[RegexValidator(regex='^[a-zA-Z]{2}[0-9]{6}[a-zA-Z]{1}$', message='National Insurance Number consists of two letters, followed by six numbers, followed by one letter.')])
email = models.EmailField(verbose_name='E-Mail', unique=True)
id_document = models.FileField(verbose_name='ID Document', upload_to='user_id_documents', help_text='E.G. Scan of passport, driving licence etc.')
address_document = models.FileField(verbose_name='Proof Of Address', upload_to='user_address_documents', help_text='E.G. Utility bill etc.')
is_validated = models.BooleanField(default=False)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['name', 'nin', 'id_document', 'address_document', 'is_validated']
class UserAddress(models.Model):
user = models.ForeignKey(CustomUser, on_delete=models.PROTECT)
name_num = models.CharField(max_length=30, null=False, unique=False, blank=False)
street = models.CharField(max_length=30, null=False, unique=False, blank=False)
postcode = models.CharField(max_length=10, null=False, unique=False, blank=False)
以及以下两种形式:
class CustomUserCreationForm(UserCreationForm):
class Meta(UserCreationForm.Meta):
model = CustomUser
fields = ('email', 'name', 'nin', 'id_document', 'address_document')
class CustomUserChangeForm(UserChangeForm):
class Meta:
model = CustomUser
fields = ('email', 'name', 'nin', 'id_document', 'address_document')
这些表单成功创建/修改了CustomUser对象,但未提及UserAddress模型。在管理员中,我使用内联方式提供/修改UserAddresses,但这不适用于注册表单。
我应该修改CustomUser使其具有UserAddress的外键吗?好像是圆形的...