0

我有以下两个模型:

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 模型。在管理员中,我使用内联来提供/修改用户地址,但这在注册表单中不起作用。

我应该修改 CustomUser 以获得 UserAddress 的外键吗?这似乎是一个循环......

4

0 回答 0