0

I have the following two models:

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)

And the following two forms:

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')

These forms successfully create/modify a CustomUser object, but do not make any mention of the UserAddress model. In the admin, I am using an inline to give/modify the UserAddresses, but this does not work in a signup form.

Should I modify CustomUser to have a foreignkey to UserAddress? This seems like it would be circular...

Alex
  • 2,270
  • 3
  • 33
  • 65
  • I think, something is wrong with your post because we don't see any forms but two times models. – Pyvonix Aug 07 '18 at 13:13
  • @Buky You're right, sorry, fixed now! – Alex Aug 07 '18 at 13:28
  • may be you have to set a `related_name` to your `user` field in UserAddress and next in you model add like this: `fields =("some fields", "user_set__street", "user_set__postcode")`. P.S. Did not test that, just idea. – Chiefir Aug 08 '18 at 08:01

0 Answers0