title: Django - After Register, Data Should Go To 2 Different Tables (Customer & User)
body: <p>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 <code>AnonymousUser</code> 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 &quot;<code>RelatedObjectDoesNotExist at / User has no customer.</code>&quot;</p>
  <p>I think the reason is that when I register, it only makes a <code>User</code> in database but didn't register anything in the <code>Customer</code> table (which consists <code>Name</code> and <code>Email</code>). How can I register a <code>Customer</code> and <code>User</code> at the same time when I hit the &quot;Submit&quot; button? And how can I make that specific User have &quot;Staff status&quot; only and cannot make changes in the Admin site?</p>
  <p>Also, I want to add new fields in the Register form for Name and Email that will go directly to the <code>Customer</code> table. I tried to do this one but it doesn't work and throwed and error that says &quot;<code>django.core.exceptions.FieldError: Unknown field(s) (name) specified for User</code>&quot;.</p>
  <p><strong>Here's what I did:</strong></p>
  <pre><code>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']
  </code></pre>
  <p><strong>SUMMARY</strong>:</p>
  <p>I want to add extra fields in the Register form called <code>Name</code> and <code>Email</code>. Then after clicking the Register form, I want create <code>User</code> and <code>Customer</code> at the same time. But the <code>User</code> should only have &quot;Staff status&quot; and cannot make changes in the Admin site. And the Name and Email field should go to <code>Customer</code> Table with the User I've created.</p>
  <p>Here's the screenshot of my <strong>Register form</strong>:
  <a href="https://i.stack.imgur.com/JswuQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JswuQ.png" alt="enter image description here" /></a></p>
  <p>Here's my <strong>forms.py</strong> file:</p>
  <pre><code>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'})
  </code></pre>
  <p>Here's my <strong>views.py</strong> file:</p>
  <pre><code>def loginUser(request):
      page = 'login'
  
      if request.method == &quot;POST&quot;:
          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 == &quot;POST&quot;:
          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)
  </code></pre>
  <p>Here's my <strong>models.py</strong> file:</p>
  <pre><code>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 
  </code></pre>
  <p>Here's my <strong>register.html</strong> file:</p>
  <pre><code>&lt;form class=&quot;form&quot; method=&quot;POST&quot;&gt;
                      {% csrf_token %}
                          &lt;h2&gt; REGISTER &lt;/h2&gt;
                          &lt;h4&gt; Create your account now! &lt;/h4&gt;
                          &lt;br /&gt;
                          {% for field in form %}
                              &lt;div class=&quot;mb-3&quot;&gt;
                                  &lt;label for=&quot;exampleInputPassword1&quot; class=&quot;form-label&quot;&gt;{{field.label}}:&lt;/label&gt;
                                  {{field}}
                              &lt;/div&gt;
                          {% endfor %}
                          &lt;button type=&quot;submit&quot; class=&quot;btn btn-primary&quot;&gt;Submit&lt;/button&gt;
                          &lt;br /&gt;
                          &lt;p&gt; Already have an account? &lt;a href=&quot;{% url 'login' %}&quot;&gt;Login here&lt;/a&gt; &lt;/p&gt;
                      &lt;/form&gt;
  </code></pre>