class Node(models.Model):
"""
节点表
"""
name = models.CharField(max_length=128, unique=True, verbose_name="节点名称")
# SlugField 是一个新闻术语(通常叫做短标题)。一个slug只能包含字母、数字、下划线或者是连字符,通常用来作为短标签。通常它们是用来放在地址栏的URL里的。
# 像CharField一样,你可以指定max_length(也请参阅该部分中的有关数据库可移植性的说明和max_length)。如果没有指定
# max_length, Django将会默认长度为50。
# 将Field.db_index设置为True。
# 根据某些其他值的值自动预填充SlugField通常很有用。你可以在admin中使用prepopulated_fields自动执行此操作。
slug = models.SlugField(max_length=128, unique=True, verbose_name="url标识符")
time_create = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
num_topics = models.IntegerField(default=0, verbose_name="主题数量")
category = models.ForeignKey(Category, on_delete=models.DO_NOTHING, verbose_name="所属类别")
show_status = models.BooleanField(default=True, verbose_name="显示状态")
def save(self, *args, **kwargs):
self.slug = slugify(self.name)
super(Node, self).save(*args, **kwargs)
class Meta:
verbose_name = "节点"
verbose_name_plural = "节点列表"
def __str__(self):
return self.name
class Article(models.Model):
"""
主题表/文章表
"""
title = models.CharField(max_length=128, unique=True, verbose_name="标题")
slug = models.SlugField(max_length=128, unique=True, verbose_name="url标识符")
content = RichTextUploadingField(verbose_name="内容", config_name='awesome_ckeditor')
node = models.ForeignKey(Node, on_delete=models.DO_NOTHING, verbose_name="所属节点")
user = models.ForeignKey(User, on_delete=models.DO_NOTHING, related_name="user_article", verbose_name="作者")
source = models.ForeignKey(Source, on_delete=models.DO_NOTHING, verbose_name="来源", blank=True, null=True)
tags = models.ManyToManyField(Tag, verbose_name="标签", related_name="tags_article", blank=True)
num_views = models.IntegerField(default=0, verbose_name="浏览数量")
num_favorites = models.IntegerField(default=0, verbose_name="收藏数量")
last_answerer = models.ForeignKey(User, on_delete=models.DO_NOTHING, related_name="last_answerer_article",
verbose_name="最后回复者", blank=True,
null=True)
show_status = models.BooleanField(default=True, verbose_name="显示状态")
time_create = models.DateTimeField(auto_now_add=True, verbose_name="发表时间")
time_update = models.DateTimeField(blank=True, null=True, auto_now=True, verbose_name="更新时间")
def save(self, *args, **kwargs):
self.slug = slugify(self.title)
super(Article, self).save(*args, **kwargs)
# 获取后台文本编辑器图文内容中图片url地址
def get_content_img_url(self):
temp = Article.objects.filter(pk=str(self.id)).values('content') # values获取Article数据表中的content字段内容
html = pq(temp[0]['content']) # pq方法获取编辑器html内容
# print(html, "\n", "----")
img_path = pq(html)('img').attr('src') # 截取html内容中的路径
# print("pic", img_path)
return img_path # 返回第一张图片路径
# 管理后台显示文章的缩略图
def thumb_shouw(self):
if self.get_content_img_url():
return format_html(
'<span><img src="{}"/>{}</span>', self.get_content_img_url(), "这里是缩略图")
else:
return format_html('<span style="color:{}">{}</span>', "red", "暂无缩略图")
thumb_shouw.short_description = format_html('<span class="text">缩略图</span>') # 新字段的显示的名称,相当于 verbose_name
thumb_shouw.admin_order_field = "-time_update" # 指定排序方式,更新时间倒序排列
class Meta:
verbose_name = "文章"
verbose_name_plural = "文章列表"
def __str__(self):
title_short = self.title if len(self.title) < 15 else self.title[:12] + '...'
return "%s %s %s" % (self.id, self.user, title_short)
class ArticleAdmin(admin.ModelAdmin):
form = ArticleAdminForm # 指定了表单,就不要再用 formfield_overrides 了
list_display = ['id', 'thumb_shouw', 'title', 'node', 'num_views', 'show_status', 'slug', 'user']
list_display_links = ['id', 'thumb_shouw', 'title', 'node', 'num_views', 'user']
list_filter = ['id', 'title', 'node__name', 'user'] # 显示外键字段,使用 字段名__关联 model 的字段名
search_fields = ['title_short', 'user', 'content']
list_editable = ["show_status", ]
# style_fields = {"content": "ueditor"}
readonly_fields = ('slug',)
show_detail_fields = ['show_status', ]