From 6b578db0bf0834d03b9f521937b02ac133e33ea3 Mon Sep 17 00:00:00 2001 From: pmh4agx8t <531334290@qq.com> Date: Sun, 12 Oct 2025 23:10:33 +0800 Subject: [PATCH] =?UTF-8?q?Add=20ccy=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ccy注释 | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 ccy注释 diff --git a/ccy注释 b/ccy注释 new file mode 100644 index 0000000..1215966 --- /dev/null +++ b/ccy注释 @@ -0,0 +1,50 @@ +# 导入Django内置的抽象用户模型(用于扩展自定义用户字段) +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 + + +# Create your models here. +# 自定义用户模型:继承AbstractUser以扩展默认用户字段 +class BlogUser(AbstractUser): + # 昵称字段:字符类型,最大长度100,允许为空 + nickname = models.CharField(_('nick name'), max_length=100, blank=True) + # 创建时间字段:DateTime类型,默认值为当前时间 + creation_time = models.DateTimeField(_('creation time'), default=now) + # 最后修改时间字段:DateTime类型,默认值为当前时间 + last_modify_time = models.DateTimeField(_('last modify time'), default=now) + # 注册来源字段:字符类型,最大长度100,允许为空(记录用户注册渠道,如“官网”“APP”) + source = models.CharField(_('create source'), max_length=100, blank=True) + + # 获取用户详情页的相对URL(通过reverse反向解析路由) + def get_absolute_url(self): + # 对应路由名称为“blog:author_detail”,传递参数“author_name”为当前用户的用户名 + return reverse( + 'blog:author_detail', kwargs={ + 'author_name': self.username}) + + # 定义用户对象的字符串表示(在后台管理/打印对象时显示邮箱) + def __str__(self): + return self.email + + # 获取用户详情页的完整绝对URL(拼接站点域名+相对URL) + def get_full_url(self): + # 获取当前站点的域名(需在Django后台配置Site) + site = get_current_site().domain + # 拼接域名与相对URL,生成完整链接 + url = "https://{site}{path}".format(site=site, + path=self.get_absolute_url()) + return url + + # 模型元信息配置 + class Meta: + # 排序规则:按id倒序(新创建的用户排在前) + ordering = ['-id'] + # 后台管理中显示的模型名称(单复数) + verbose_name = _('user') + verbose_name_plural = verbose_name + # 指定按id字段获取最新记录 + get_latest_by = 'id'