Interestingly, I cloned the source code from Github and setup the project. I can actually connect in the admin dashboard, but with my email, and not the username.
I think that I created this confusion tho as I only wanted the user to connect with the email.
In the User model, you can set these attributes so any user can connect with username and email.
Hi Mangambo, i made the changes. However was still facing the same issue. I decided to change the Model as of below and it worked.
import uuid
from enum import unique
from django.db import models
from django.conf import settings
from django.utils import timezone
from django.core.mail import send_mail
from django.core.validators import RegexValidator
from dateutil.relativedelta import relativedelta
from django.utils.translation import gettext_lazy as _
from phonenumber_field.modelfields import PhoneNumberField
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin, BaseUserManager
def two_days_from_now():
# A helper function to deactivate email activation link after 2 days
return timezone.now() + relativedelta(days=2)
class UserManager(BaseUserManager):
"""
We use Django's inbuilt BaseUserManager Class
"""
def _create_user(self, username, email, password, is_staff, is_superuser, **extra_fields):
"""
Creates and saves a User with the given email and password.
"""
now = timezone.now()
if not email:
raise ValueError('Users must have an email address')
if not username:
raise ValueError('Users must have a username')
email = self.normalize_email(email)
user = self.model(username=username,
email=email,
is_staff=is_staff,
is_active=True,
is_superuser=is_superuser,
last_login=now,
date_joined=now,
**extra_fields)
user.set_password(password)
user.save(using=self._db)
return user
def create_user(self, username, email, password=None, **extra_fields):
return self._create_user(username, email, password, False, False, **extra_fields)
def create_superuser(self, username, email, password, **extra_fields):
return self._create_user(username, email, password, True, True, **extra_fields)
class User(AbstractBaseUser, PermissionsMixin):
# The base Zetech user's model fields
alphanumeric = RegexValidator(r'^[0-9a-zA-Z]*$', message='Only alphanumeric characters are allowed.')
### Redefine the basic fields that would normally be defined in User ###
username = models.CharField(unique=True, max_length=20, validators=[alphanumeric])
email = models.EmailField(verbose_name='email address', unique=True, max_length=255)
first_name = models.CharField(max_length=30, null=True, blank=True)
last_name = models.CharField(max_length=50, null=True, blank=True)
date_joined = models.DateTimeField(auto_now_add=True)
is_active = models.BooleanField(default=True,
null=False,
help_text=_('Designates whether this user should be treated as active. Unselect this instead of deleting accounts.'))
is_staff = models.BooleanField(default=False,
null=False,
help_text=_('Designates whether the user can log into this admin site.'))
### NOTE: is_superuser is not defined here. This is because PermissionsMixin already defines this, and if you override it,
### then all the default Django user permissions won't work correctly, so you have to make sure you don't override the
### is_superuser field.
### Our own fields - we shall use this for the user account profile ###
phone_number = PhoneNumberField()
profile_image = models.ImageField(upload_to="uploads", blank=False, null=False, default="/static/images/defaultuserimage.png")
user_bio = models.CharField(max_length=600, blank=True)
is_uni_staff = models.BooleanField(default=False,
null=False,
help_text=_('Designates whether user is a staff of the university'))
is_student = models.BooleanField(default=False,
null=False,
help_text=_('Designates whether user is a student in the university'))
objects = UserManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['username']
class Meta:
verbose_name = 'User'
verbose_name_plural = 'Users'
def get_full_name(self):
fullname = "{first_name} {last_name}".format(first_name=self.first_name, last_name=self.last_name)
return fullname
def get_short_name(self):
return self.username
def __str__(self):
return "{0} ({1})".format(self.username, self.email)
def email_user(self, subject, message, from_email=None):
"""
Sends an email to this User.
"""
send_mail(subject, message, from_email, [self.email])
class Registration(models.Model):
uuid = models.UUIDField(primary_key=False, default=uuid.uuid4, editable=False)
user = models.OneToOneField(User, related_name='registration', on_delete=models.CASCADE)
expires = models.DateTimeField(default=two_days_from_now)
type = models.CharField(max_length=10, choices=(
('register', 'register'),
('lostpass', 'lostpass'),
), default = 'register')
Thanks for the article.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Interestingly, I cloned the source code from Github and setup the project. I can actually connect in the admin dashboard, but with my
email
, and not theusername
.I think that I created this confusion tho as I only wanted the user to connect with the email.
In the
User
model, you can set these attributes so any user can connect with username and email.Please, let me know if it works.
Hi Mangambo, i made the changes. However was still facing the same issue. I decided to change the Model as of below and it worked.
Thanks for the article.