update. 优化模板结构
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
from django.core.cache import cache
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.db import models
|
||||
|
||||
@@ -49,6 +50,42 @@ class FriendLink(BaseEntity):
|
||||
return f"[{self.category.name}] {self.name}"
|
||||
|
||||
|
||||
class PictureLink(BaseEntity):
|
||||
name = models.CharField("网站名称", max_length=100)
|
||||
picture = models.ImageField("图片链接", upload_to='picture/%Y/%m/%d/', help_text="建议尺寸:280x120")
|
||||
url = models.URLField("链接地址", help_text="点击图片要访问的站点地址")
|
||||
|
||||
# 2. 排序和状态
|
||||
order = models.IntegerField("排序", default=0)
|
||||
is_active = models.BooleanField("是否启用", default=True)
|
||||
|
||||
class Meta:
|
||||
verbose_name = "图片链接"
|
||||
verbose_name_plural = "图链管理"
|
||||
ordering = ['order', '-created_at']
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.name}"
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
# 1. 逻辑判断:如果当前想要启用
|
||||
if self.is_active:
|
||||
# 统计现有的启用数量(排除自己)
|
||||
active_count = PictureLink.objects.filter(is_active=True).exclude(pk=self.pk).count()
|
||||
|
||||
# 2. 如果已满 4 个
|
||||
if active_count >= 4:
|
||||
self.is_active = False # 强制设为不生效
|
||||
|
||||
# 3. 【关键】把消息暂存起来,key 用 pk(如果是新建还没 pk,用 id(self) 临时顶替)
|
||||
# 注意:这里不能直接弹窗,因为没有 request
|
||||
msg_key = self.pk if self.pk else id(self)
|
||||
msg = "限制生效:最多只能有 4 个启用的链接,当前对象已被自动设置为【不启用】。"
|
||||
cache.set(f"picture_count_limit_{msg_key}", msg) # 缓存 30 秒
|
||||
|
||||
super().save(*args, **kwargs)
|
||||
|
||||
|
||||
class MainMenu(BaseEntity):
|
||||
# --- 基础信息 ---
|
||||
title = models.CharField("标题", max_length=100)
|
||||
|
||||
Reference in New Issue
Block a user