title: Django - Trouble adding User to User Group with custom User Model
body: <p>Previously, I have been using the default Django user model and have been adding users to specific user groups when they register with no problem using this code in my views.py: </p>
  
  <p><code>user.save()
   user.groups.add(Group.objects.get(name='customers'))</code></p>
  
  <p>However, I have now changed to a custom user model so I can remove the 'username' field in my register form and now this code no longer works. This error message is being thrown when new users try to register:</p>
  
  <p>'User' object has no attribute 'groups'</p>
  
  <p>I have searched but can't find an answer to this question. I am very new to working on the backend with Django so please be very descriptive in your answers (i.e where I need to put the code you suggest, models.py/views.py/forms.py etc). Any help would be much appreciated!</p>
  
  <p>My models.py:</p>
  
  <pre><code>from django.db import models
  from django.contrib.auth.models import (
      AbstractBaseUser, BaseUserManager
  )
  
  class UserManager(BaseUserManager):
      def create_user(self, email, first_name, last_name, password=None, is_staff=False, is_admin=False):
          if not email:
              raise ValueError("Users must enter an email")
  
          if not password:
              raise ValueError("Users must enter a password")
  
          if not first_name:
              raise ValueError("Users must enter their first name")
  
          if not last_name:
              raise ValueError("Users must enter their last name")
  
          user_obj = self.model(
              email = self.normalize_email(email),
              first_name = first_name,
              last_name = last_name,
          )
          user_obj.set_password(password) #set and change password?
          user_obj.admin = is_admin
          user_obj.staff = is_staff
          user_obj.save(using=self._db)
          return user_obj
  
      def create_superuser(self, email, first_name, last_name, password=None):
          user = self.create_user(
              email,
              first_name,
              last_name,
              password=password,
              is_staff=True,
              is_admin=True
          )
          return user
  
  class User(AbstractBaseUser):
      first_name = models.CharField(max_length=255, blank=True, null=True)
      last_name = models.CharField(max_length=255, blank=True, null=True)
      email = models.EmailField(max_length=255, unique=True)
      timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
      # active = models.BooleanField(default=True) #can login
      staff = models.BooleanField(default=False) #staff not superuser
      admin = models.BooleanField(default=False) #superuser
  
      USERNAME_FIELD = 'email'
  
      REQUIRED_FIELDS = ['first_name', 'last_name']
  
      objects = UserManager()
  
      def __str__(self):
          return self.email
  
      def get_first_name(self):
          return self.email
  
      def get_last_name(self):
          return self.email
  
  
      def get_short_name(self):
          return self.email
  
      def has_perm(self, perm, obj=None):
          return True
  
      def has_module_perms(self, app_label):
          return True
  
      @property
      def is_staff(self):
          return self.staff
  
      @property
      def is_admin(self):
          return self.admin
  </code></pre>
  
  <p>formy.py</p>
  
  <pre><code>from django import forms
  from django.contrib.auth import (
      authenticate,
      get_user_model,
      login,
      logout,
      )
  from django.contrib.auth.forms import ReadOnlyPasswordHashField
  from django.contrib.auth.models import User, Group
  
  
  User = get_user_model()
  
  class UserAdminCreationForm(forms.ModelForm):
      """A form for creating new users. Includes all the required
      fields, plus a repeated password."""
      password1 = forms.CharField(label='Password', widget=forms.PasswordInput)
      password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput)
  
      class Meta:
          model = User
          fields = ('email', 'first_name', 'last_name')
  
      def clean_password2(self):
          # Check that the two password entries match
          password1 = self.cleaned_data.get("password1")
          password2 = self.cleaned_data.get("password2")
          if password1 and password2 and password1 != password2:
              raise forms.ValidationError("Passwords don't match")
          return password2
  
      def save(self, commit=True):
          # Save the provided password in hashed format
          user = super(UserAdminCreationForm, self).save(commit=False)
          user.set_password(self.cleaned_data["password1"])
          if commit:
              user.save()
          return user
  </code></pre>
  
  <p>views.py</p>
  
  <pre><code>from __future__ import unicode_literals
  from barbers.forms import BarberProfileForm
  from barbers.models import BarberProfile
  from django.contrib.auth.models import User, Group
  from django.contrib.auth import (
      authenticate,
      get_user_model,
      login,
      logout,
      )
  from django.contrib.auth.decorators import login_required
  from django.http import HttpResponse, HttpResponseRedirect
  from django.shortcuts import redirect, render, get_object_or_404
  from .forms import BarberRegisterForm, UserLoginForm, UserRegisterForm, UserAdminCreationForm
  
  def register_view(request):
  
      print(request.user.is_authenticated())
  
      register_title = "Register"
      register_form = UserAdminCreationForm(request.POST or None)
      if register_form.is_valid():
          user = register_form.save(commit=False)
          password = register_form.cleaned_data.get('password')
          user.set_password(password)
          user.save()
          user.groups.add(Group.objects.get(name='customers'))
          new_user = authenticate(username=user.username, password=password)
          login(request, new_user)
          if next:
              return redirect(next)
          return redirect("/my-barbers/{0}".format(request.user.id), user=request.user)
  
      context = {
          "register_form": register_form,
          "register_title": register_title,
      }
  
      return render (request, "login.html", context)
  </code></pre>