Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

blog.models的模型文件中的class SideBar(models.Model)模型在后台管理页面中修改或增加新侧边栏设置失效 #735

Open
4 of 6 tasks
JOYCAT-Q opened this issue Jul 19, 2024 · 0 comments

Comments

@JOYCAT-Q
Copy link

我确定我已经查看了 (标注[ ][x])


我要申请 (标注[ ][x])

  • BUG 反馈
  • 添加新的特性或者功能
  • 请求技术支持

blog.models中的class SideBar(models.Model)

class SideBar(models.Model):
    """侧边栏,可以展示一些html内容"""
    name = models.CharField(_('title'), max_length=100)
    content = models.TextField(_('content'))
    sequence = models.IntegerField(_('order'), unique=True)
    is_enable = models.BooleanField(_('is enable'), default=True)
    """应当继承baseModel基类感觉更好"""
    creation_time = models.DateTimeField(_('creation time'), default=now)
    last_mod_time = models.DateTimeField(_('modify time'), default=now)

    class Meta:
        ordering = ['sequence']
        verbose_name = _('sidebar')
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

BUG表现:当使用管理员账号在后台添加新的或修改已有侧边栏数据时,站点页面并不会及时更新,使得侧边栏数据设置失效

失效原因:
在当前项目中,使用了cache,当已存在的侧边栏被修改或新增侧边栏时,但旧的侧边栏数据已经存在于cache当中,
每次重新获取都会从cache中得到,从而导致并非最新数据,那么这个修改不会立即生效。

解决方法:在class SideBar(models.Model)中重构save方法

class SideBar(models.Model):
    """侧边栏,可以展示一些html内容"""
    # 其他不变
    def save(self, *args, **kwargs):
        super().save(*args, **kwargs)
        from djangoblog.utils import cache
        # 每次保存新的侧边栏数据时清除cache,让其重新从数据库加载最新数据
        cache.clear()

即可修复每次更改侧边栏数据无法即使更新问题

当前大体环境配置:
Django==4.2.14
Windows10
MySQL==8.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant