Need Help with Dajngo Models

hi, I’m working on a Django project and I’m struggling with a bug, id like for the information on my registration form to fill in both the user and customer models simultaneously in the Django database upon registration. thanks in advance :slight_smile:
my models.py:

class Customer(models.Model):
    user = models.OneToOneField(User, null=True, blank=True, on_delete=models.CASCADE)
    name = models.CharField(max_length=200, null=True)
    last_name = models.CharField(max_length=200, null=True)
    email = models.CharField(max_length=200, null=True)

forms.py

class CustomerForm(ModelForm):
    class Meta:
        model = Customer
        fields = ['name', 'last_name', 'email']


class CreateUserForm(UserCreationForm):
    class Meta:
        model = User
        fields = ['username', 'first_name',  'last_name', 'email','password1', 'password2']

views.py

def register(request):
form = CreateUserForm()
if request.method == ‘POST’:
form = CreateUserForm(request.POST)
if form.is_valid():
form.save()
return redirect(‘login’)

context = {'form': form}
return render(request, 'items/register.html', context)

Since you’re only adding one field (name) with the customer model, it probably would have been easier to go with a custom user model and have all the fields on one model, or to write a method that generates the name from the first and last name fields on the user model and just use it.

If you’re committed to two though, there are some examples (first, second) at SO that may be helpful.