CustomUser for "users" auth.User for superusers

Hi there,

I am in the process of adding a custom user model to my app and upon doing so I am facing some difficulties. I’d like to add some fields for my CustomUser:

the relevant part in models.py:

class CustomGroup(Group):
    # Define your custom fields here

    class Meta:
        proxy = True
class CustomUser(AbstractUser):
    class Meta:
        verbose_name = 'Custom User'
        verbose_name_plural = 'Custom Users'

    groups = models.ManyToManyField(
        CustomGroup,
        verbose_name='groups',
        blank=True,
        help_text='The groups this user belongs to.',
        related_name='custom_user_groups'  # Unique related name
    )
    user_permissions = models.ManyToManyField(
        CustomPermission,
        verbose_name='user permissions',
        blank=True,
        help_text='Specific permissions for this user.',
        related_name='custom_user_permissions'  # Unique related name
    )

class UserProfile(models.Model):
    user = models.OneToOneField(CustomUser, on_delete=models.CASCADE)
    learned_words = models.ManyToManyField('Word')

while I am contempt with using the regular auth.User for my superuser, for now.

I have the following in settings:

AUTH_USER_MODEL = "VocabTrainer.CustomUser"

if I change this to auth.User I can login using my superuser, which makes sense. If I have it like the above I cannot login with the created users, anyways …

urls.py

from django.conf import settings
from django.conf.urls.static import static
import django.contrib.auth.urls


urlpatterns = [
    path('', IndexView.as_view(), name='index'),
    path('word-list/', WordListView.as_view(), name='word-list-view'),
    path('word-list/<str:language>/', LanguageWordListView.as_view(), name='lang-word-list-view'),
    path('word-list/<str:language>/<int:pk>', WordFormView.as_view(), name='word-form'),
    path('languages', LanguageList.as_view(), name='language-list'),
    path('accounts/signup', SignUpView.as_view(), name='register'),
    path('accounts/profile/<int:pk>/', UserProfileView.as_view(), name='profile'),
    path('upload_csv', UploadCSVView.as_view(), name='upload-csv')
    ]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

so, I want to have a form (templates/login.html) where I can either login with superuser or with regular user and it’s authenticated respectively.
The form is working, but only with AUTH_USER_MODEL = 'auth.User'

my form.py


{% extends "VocabTrainer/templates/base.html" %}

{% block content %}

  {% if form.errors %}
    <p>Your username and password didn't match. Please try again.</p>
  {% endif %}

  {% if next %}
    {% if user.is_authenticated %}
      <p>Your account doesn't have access to this page. To proceed,
      please login with an account that has access.</p>
    {% else %}
      <p>Please login to see this page.</p>
    {% endif %}
  {% endif %}

  <form method="post" action="{% url 'login' %}">
    {% csrf_token %}
    <table>
      <tr>
        <td>{{ form.username.label_tag }}</td>
        <td>{{ form.username }}</td>
      </tr>
      <tr>
        <td>{{ form.password.label_tag }}</td>
        <td>{{ form.password }}</td>
      </tr>
    </table>
    <input type="submit" value="login">
    <input type="hidden" name="next" value="{{ next }}">
  </form>

  {# Assumes you setup the password_reset view in your URLconf #}
  <p><a href="{% url 'password_reset' %}">Lost password?</a></p>

{% endblock %}

can anyone help me with that?

all the best and thanks to everybody who reads this post and thinks about it!

The first thing to be aware of is that changing the User model after you have initialized your database and run your initial migrations creates a number of issues.

If you are changing from the default user to your own custom user, the quick way to handle this is to drop and recreate your database, so that your initial migrate knows to use your new model. (Review the docs for Substituting a custom User model)

This is not something you can swap in and out arbitrarily.

This also means that you will only have one type of user. A user is identified as a “superuser” by having the is_superuser field set to true.

Now, if you have done this - then we’ll need to know more about what you mean by:

What errors or behavior are you seeing? Are there any messages on the runserver console? Are any errors being returned by the login process to the login form? What steps have you taken to try and debug this?

No errors or anything…
I will update this later.
We are, since yesterday, dealing with two nice cases of dengue, and that nasty virus is taking our attention …

(Didn’t want to be rude and keep you waiting forever, but now I’m off and to bed again)

Hope y’all get better soon. No worries about any delays, we’re here for you.