0

I'm trying to make a registration form for a extension of django's user model. It has a two relationships, one to User and Address. The form needs to have all the fields from User, UserDetails and Address. But i'm struggling to get the correct view and form. Just having a ModelForm for UserDetails combined with FormView doesn't add the fields for User and Address.

class User(AbstractBaseUser, PermissionMixin):
    email = models.EmailField(unique=True)


class UserDetails(model.Model):
    date_of_birth = models.DateField()
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    address = models.OneToOneField(Address, on_delete=models.CASCADE)

class Address(model.Model):
    field = models.CharField(max_length=100)
    field2 = models.CharField(max_length=100)

The form and view look like this:

class UserRegistrationForm(ModelForm):
    class Meta:
        model = Orchestra
        fields = '__all__'

class UserRegistrationView(FormView):
    form_class = UserRegistrationForm
    template_name = 'users/register.html'

<form action="" method="post">
    {% csrf_token %}
    {{ form.as_table }}
    <input type="submit" value="submit">
</form>
user6827
  • 1,576
  • 5
  • 19
  • 25

2 Answers2

0

You missed the unicode or str method declaration in model classes. You should always declare it. Remember that str is for python 3.x, and unicode for python 2.x.

Here is an example for class Address and python 2:

def __unicode__(self):
    return '%s %s' % (self.field1, self.field2)
0

I had to create different forms for each model. Then combine all the instances created from the forms. See this answer for more details

Community
  • 1
  • 1
user6827
  • 1,576
  • 5
  • 19
  • 25