1. https://cloud.tencent.com/developer/ask/34273
2. https://djangosnippets.org/snippets/2885/
4. https://blog.csdn.net/kkevinyang/article/details/79501576
from django.db import models
from django.db.models.signals import post_save
from django.contrib.auth import get_user_model
from django.template.defaultfilters import slugify
from DjangoUeditor.models import UEditorField
from pyquery import PyQuery as pq # pip install pyquery,获取到html中的img图片地址返回
from pypinyin import lazy_pinyin
User = get_user_model()
# Create your models here.
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标识符")
create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
num_topics = models.IntegerField(default=0, verbose_name="主题数量")
category = models.ForeignKey(Category, verbose_name="所属类别")
is_show = models.BooleanField(default=True, verbose_name="显示状态")
def save(self, *args, **kwargs):
self.slug = slugify("-".join(lazy_pinyin(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, verbose_name="标题")
slug = models.SlugField(max_length=128, unique=True, verbose_name="url标识符")
content = UEditorField(verbose_name="内容", imagePath="blog/images/", width=1000, height=300,
filePath="blog/files/", default='')
node = models.ForeignKey(Node, verbose_name="所属节点")
user = models.ForeignKey(User, related_name="user_article", verbose_name="作者")
source = models.ForeignKey(Source, 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, related_name="last_answerer_article", verbose_name="最后回复者", blank=True,
null=True)
is_show = models.BooleanField(default=True, verbose_name="显示状态")
create_time = models.DateTimeField(auto_now_add=True, verbose_name="发表时间")
update_time = models.DateTimeField(blank=True, null=True, auto_now=True, verbose_name="更新时间")
def save(self, *args, **kwargs):
self.slug = slugify("-".join(lazy_pinyin(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 # 返回第一张图片路径
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)
NodeFilter 主要步骤:
1. 继承 SimpleListFilter
2. 修改 lookups,lookups 用来显示在网页上的筛选条件
3. 修改 queryset,根据传进来的值来返回查询结果
from django.contrib import admin
from django.forms import widgets
from apps.blog.models import *
from apps.blog.forms import ArticleAdminForm
from django.contrib.admin import SimpleListFilter
class NodeFilter(SimpleListFilter):
title = 'node' # or use _('country') for translated title
parameter_name = 'node'
def lookups(self, request, model_admin):
# 查出 node 的 id 和 name 值 用来显示在网页上的筛选条件
nodes = Node.objects.all()
return [(node.id, node.name + "-自定义") for node in nodes]
def queryset(self, request, queryset):
if self.value():
# 筛选条件有值时, 查询对应的 node 的文章,用 title 正排序
return queryset.filter(node__id=self.value()).order_by("title")
else:
# 筛选条件没有值时,全部的时候是没有值的
return queryset
class ArticleAdmin(admin.ModelAdmin):
form = ArticleAdminForm # 指定了表单,就不要再用 formfield_overrides 了
list_display = ['id', 'thumb_shouw', 'title', 'node', 'num_views', 'show_status', 'slug', 'user', 'time_create']
list_display_links = ['id', 'thumb_shouw', 'title', 'node', 'num_views', 'user']
list_filter = ['id', 'source__name', NodeFilter] # 把自己的筛选添加加进来
search_fields = ['title_short', 'user', 'content']
list_editable = ["show_status", ]
# style_fields = {"content": "ueditor"}
readonly_fields = ('slug',)
show_detail_fields = ['show_status', ]