'Django - After Register, Data Should Go To 2 Different Tables (Customer & User)

I am creating an e-commerce website where people can choose to login or not but still the can order and checkout (even if you are an AnonymousUser or Guest user). Now, I am making a login and register form in my website. The login form works and looks good but the register form wasn't working and throwing an error that said "RelatedObjectDoesNotExist at / User has no customer."

I think the reason is that when I register, it only makes a User in database but didn't register anything in the Customer table (which consists Name and Email). How can I register a Customer and User at the same time when I hit the "Submit" button? And how can I make that specific User have "Staff status" only and cannot make changes in the Admin site?

Also, I want to add new fields in the Register form for Name and Email that will go directly to the Customer table. I tried to do this one but it doesn't work and throwed and error that says "django.core.exceptions.FieldError: Unknown field(s) (name) specified for User".

Here's what I did:

from django.forms import ModelForm 
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm 
from .models import *

class CustomUserCreationForm(UserCreationForm):
    name = models.CharField(max_length=200, null=True)
    email = models.CharField(max_length=200)

    class Meta:
        model = User
        fields = ['username', 'name', 'email', 'password1', 'password2']

SUMMARY:

I want to add extra fields in the Register form called Name and Email. Then after clicking the Register form, I want create User and Customer at the same time. But the User should only have "Staff status" and cannot make changes in the Admin site. And the Name and Email field should go to Customer Table with the User I've created.

Here's the screenshot of my Register form: enter image description here

Here's my forms.py file:

from django.forms import ModelForm 
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm 

class CustomUserCreationForm(UserCreationForm):

    class Meta:
        model = User
        fields = ['username', 'password1', 'password2']

    def __init__(self, *args, **kwargs):
        super(CustomUserCreationForm, self).__init__(*args, **kwargs)
        self.fields['username'].widget.attrs.update({'class':'form-control','placeholder':'Enter Username'})
        self.fields['password1'].widget.attrs.update({'class':'form-control','placeholder':'Enter Password'})
        self.fields['password2'].widget.attrs.update({'class':'form-control','placeholder':'Confirm Password'})

Here's my views.py file:

def loginUser(request):
    page = 'login'

    if request.method == "POST":
        username = request.POST['username']
        password = request.POST['password']

        user = authenticate(request, username=username, password=password)
        
        print('USER:', user)

        if user is not None:
            login(request, user)
            return redirect('/')

    return render(request, 'store/login_register.html', {'page': page})

def logoutUser(request):
    logout(request)
    return redirect('/')

def registerUser(request):
    page = 'register'
    form = CustomUserCreationForm()

    if request.method == "POST":
        form = CustomUserCreationForm(request.POST)
        if form.is_valid():
            user = form.save(commit=False)
            user.save()

            user = authenticate(request, username=user.username, password=request.POST['password1'])

            if user is not None:
                login(request, user)
                return redirect('/')

    context = {'form': form, 'page': page}
    return render(request, 'store/login_register.html', context)

Here's my models.py file:

from django.db import models
from django.contrib.auth.models import User

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

    def __str__(self):
        return self.name 

Here's my register.html file:

<form class="form" method="POST">
                    {% csrf_token %}
                        <h2> REGISTER </h2>
                        <h4> Create your account now! </h4>
                        <br />
                        {% for field in form %}
                            <div class="mb-3">
                                <label for="exampleInputPassword1" class="form-label">{{field.label}}:</label>
                                {{field}}
                            </div>
                        {% endfor %}
                        <button type="submit" class="btn btn-primary">Submit</button>
                        <br />
                        <p> Already have an account? <a href="{% url 'login' %}">Login here</a> </p>
                    </form>


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source