You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
https/src/DjangoBlog-master/accounts/models.py

38 lines
1.4 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#ht: 用户数据模型模块,定义自定义用户模型
from django.contrib.auth.models import AbstractUser
from django.db import models
from django.urls import reverse
from django.utils.timezone import now
from django.utils.translation import gettext_lazy as _
from djangoblog.utils import get_current_site
class BlogUser(AbstractUser):
"""ht: 博客用户模型扩展Django默认用户模型"""
nickname = models.CharField(_('nick name'), max_length=100, blank=True)
creation_time = models.DateTimeField(_('creation time'), default=now)
last_modify_time = models.DateTimeField(_('last modify time'), default=now)
source = models.CharField(_('create source'), max_length=100, blank=True)
def get_absolute_url(self):
"""ht: 获取用户详情页URL"""
return reverse(
'blog:author_detail', kwargs={
'author_name': self.username})
def __str__(self):
"""ht: 字符串表示,返回邮箱"""
return self.email
def get_full_url(self):
"""ht: 获取完整的用户URL包含域名"""
site = get_current_site().domain
url = "https://{site}{path}".format(site=site,
path=self.get_absolute_url())
return url
class Meta:
ordering = ['-id'] # ht: 默认按ID倒序排列
verbose_name = _('user')
verbose_name_plural = verbose_name
get_latest_by = 'id'