Reputation: 82
admin dashboard:
In addition to the admin part, the template is displayed in the same way.
Ever since I customized the accounts section, in all the sections where I have used the username, there is a problem that the usernames are displayed without that name and only by displaying the phrase username.
settings.py:
AUTH_USER_MODEL = 'accounts.CustomUser'
models.py(accounts):
class MyAccountManager(BaseUserManager):
def create_user(self, email, username, password=None):
if not email:
raise ValueError("Users must have an email address.")
if not username:
raise ValueError("Users must have a username.")
user = self.model(
email=self.normalize_email(email),
username=username
)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, email, username, password):
user = self.create_user(
email=self.normalize_email(email),
username=username,
password=password,
)
user.is_admin = True
user.is_staff = True
user.is_superuser = True
user.save(using=self._db)
return user
def get_profile_image_filepath(self, filepath):
return f'images/accounts/profiles/{self.pk}/{"profile.png"}'
class CustomUser(AbstractBaseUser, PermissionsMixin):
class Meta:
permissions = [
('all', 'all of the permissions')
]
first_name = models.CharField(max_length=30, null=True, blank=True)
last_name = models.CharField(max_length=30, null=True, blank=True)
email = models.EmailField(verbose_name='email', max_length=100, unique=True)
username = models.CharField(max_length=55, unique=True)
date_joined = models.DateTimeField(verbose_name='date joined', auto_now_add=True)
last_login = models.DateTimeField(verbose_name='last login', auto_now=True)
is_admin = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)
is_superuser = models.BooleanField(default=False)
profile_image = models.ImageField(null=True, blank=True, upload_to=get_profile_image_filepath, default='images/accounts/profiles/default_image.jpg')
objects = MyAccountManager()
USERNAME_FIELD = 'username'
REQUIRED_FIELDS = ['email']
def __str__(self):
return self.USERNAME_FIELD
def get_profile_image_filename(self):
return str(self.profile_image)[str(self.profile_image).index(f'images/accounts/profiles/{self.pk}/'):]
def get_absolute_url(self):
return reverse("accounts:user_view", args=[str(self.id)])
models.py(news):
class News(models.Model):
class Meta:
permissions = [
('all', 'all of the permissions')
]
ordering = ['-date']
title = models.CharField(max_length=255)
header_image = models.ImageField(null=True, blank=True, upload_to="images/news/header/")
body = RichTextUploadingField()
date = models.DateTimeField(auto_now_add=True)
author = models.ForeignKey(
AUTH_USER_MODEL,
on_delete=models.CASCADE,
)
category = models.ManyToManyField(Category, default='cryptocurrency', related_name='category')
like_news = models.ManyToManyField(AUTH_USER_MODEL, blank=True, related_name='the_news')
unlike_news = models.ManyToManyField(AUTH_USER_MODEL, blank=True, related_name='the_news_unlike')
def total_likes(self):
return self.like_news.count()
def total_unlikes(self):
return self.unlike_news.count()
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse("news_detail", args=[str(self.id)])
Which part of the code is wrong that shows the username like this?
Upvotes: 1
Views: 52
Reputation: 476557
You need to return self.username
, not :self.USERNAME_FIELD
class CustomUser(AbstractBaseUser, PermissionsMixin):
# …
USERNAME_FIELD = 'username'
def __str__(self):
return self.username
or if you want to return the attribute with the USERNAME_FIELD
, you can use the getattr(…)
function [Python-doc]:
class CustomUser(AbstractBaseUser, PermissionsMixin):
# …
USERNAME_FIELD = 'username'
def __str__(self):
return getattr(self, self.USERNAME_FIELD)
Upvotes: 3