Django authentication and social-sign on with django-allauth

Create a custom UserManager to use the email field in Django

Want more?

This lesson for enrolled students only. Join the course to unlock it!

You can see the code changes implemented in this lecture below.

If you have purchased the course in a different platform, you still have access to the code changes per lecture here on Teclado. The lecture video and lecture notes remain locked.
Join course for $15

New files

app/managers.py
from django.contrib.auth.models import BaseUserManager
from django.utils.translation import gettext_lazy as _


class UserProfileManager(BaseUserManager):
    def create_user(self, email, password=None, **extra_fields):
        if not email:
            raise ValueError(_("The Email must be set"))
        email = self.normalize_email(email)
        user = self.model(email=email, **extra_fields)
        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_superuser(self, email, password=None, **extra_fields):
        extra_fields.setdefault("is_staff", True)
        extra_fields.setdefault("is_superuser", True)

        if extra_fields.get("is_staff") is not True:
            raise ValueError(_("Superuser must have is_staff=True"))
        if extra_fields.get("is_superuser") is not True:
            raise ValueError(_("Superuser must have is_superuser=True"))

        return self.create_user(email, password, **extra_fields)

Modified files

locale/es/LC_MESSAGES/django.po
--- 
+++ 
@@ -8,7 +8,7 @@
 msgstr ""
 "Project-Id-Version: PACKAGE VERSION\n"
 "Report-Msgid-Bugs-To: \n"
-"POT-Creation-Date: 2024-11-15 12:54+0000\n"
+"POT-Creation-Date: 2024-11-15 13:00+0000\n"
 "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
 "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
 "Language-Team: LANGUAGE <LL@li.org>\n"
@@ -19,42 +19,58 @@
 "Plural-Forms: nplurals=3; plural=n == 1 ? 0 : n != 0 && n % 1000000 == 0 ? "
 "1 : 2;\n"

-#: app/models.py:23
+#: app/managers.py:8
+msgid "The Email must be set"
+msgstr "El correo electrónico debe estar establecido"
+
+#: app/managers.py:20
+msgid "Superuser must have is_staff=True"
+msgstr "El superusuario debe tener is_staff=True"
+
+#: app/managers.py:22
+msgid "Superuser must have is_superuser=True"
+msgstr "El superusuario debe tener is_superuser=True"
+
+#: app/models.py:18
+msgid "email address"
+msgstr "dirección de correo electrónico"
+
+#: app/models.py:29
 msgid "Article"
 msgstr "Artículo"

-#: app/models.py:24
+#: app/models.py:30
 msgid "Articles"
 msgstr "Artículos"

-#: app/models.py:26
+#: app/models.py:32
 msgid "title"
 msgstr "título"

-#: app/models.py:27
+#: app/models.py:33
 msgid "content"
 msgstr "contenido"

-#: app/models.py:28
+#: app/models.py:34
 msgid "word count"
 msgstr "recuento de palabras"

-#: app/models.py:29
+#: app/models.py:35
 msgid "twitter post"
 msgstr "post de twitter"

-#: app/models.py:31
+#: app/models.py:37
 msgid "status"
 msgstr "estado"

-#: app/models.py:36
+#: app/models.py:42
 msgid "created at"
 msgstr "creado en"

-#: app/models.py:37
+#: app/models.py:43
 msgid "updated at"
 msgstr "actualizado en"

-#: app/models.py:40
+#: app/models.py:46
 msgid "creator"
 msgstr "creador"
app/models.py
--- 
+++ 
@@ -5,6 +5,7 @@
 from django.contrib.auth.models import AbstractUser
 from django.utils.translation import gettext_lazy as _

+from app.managers import UserProfileManager

 ARTICLE_STATUS = (
     ("draft", "draft"),
@@ -14,7 +15,12 @@


 class UserProfile(AbstractUser):
-    pass
+    email = models.EmailField(_("email address"), max_length=255, unique=True)
+
+    objects = UserProfileManager()
+
+    USERNAME_FIELD = "email"
+    REQUIRED_FIELDS = []


 class Article(models.Model):
djangocourse/settings.py
--- 
+++ 
@@ -149,7 +149,7 @@
 # Internationalization
 # https://docs.djangoproject.com/en/5.1/topics/i18n/

-LANGUAGE_CODE = 'es'  # remember to change this back to en-us!
+LANGUAGE_CODE = 'en-us'  # remember to change this back to en-us!

 TIME_ZONE = 'UTC'