提供一个参考,具体逻辑要看具体情况。本质就是根据条件展示不同的表单内容(注意表单必填项字段,保存时要记得传值)。
from django.contrib import admin
from blog.models import *
from blog.forms import ArticleAddForm, ArticleSupperAddForm
class ArticleAdmin(admin.ModelAdmin):
class Media:
css = {
"all": ("static/styles/monokai_sublime.css",)
}
# js = ("my_code.js",)
def get_list_display(self, request):
"""
Return a sequence containing the fields to be displayed on the
changelist.
"""
# 一个model可能有很多的字段,但不可能全部显示出来,这样页面肯定不好看,而且不同的用户对字段的侧重点是不同的。
# if request.user == 'A':
# self.list_display = ('AA', 'AB', 'AC')
# else:
# self.list_display = ('AA', 'AB')
#
return self.list_display
def get_queryset(self, request):
"""
根据用户返回文章列表
:param request:
:return:
"""
qs = super().get_queryset(request)
if request.user.is_superuser:
return qs
return qs.filter(author=request.user)
def get_form(self, request, obj, **kwargs):
"""
根据用户返回表单
:param request:
:param obj:
:param kwargs:
:return:
"""
# print("obj.author1", obj.author)
# print("request1", request.user)
if request.user.is_superuser:
kwargs['form'] = ArticleSupperAddForm
else:
kwargs['form'] = ArticleAddForm
return super(ArticleAdmin, self).get_form(request, obj, **kwargs)
def save_model(self, request, obj, form, change):
"""
根据登录的用户,自动添加新建文章的作者
:param request:
:param obj:
:param form:
:param change:
:return:
"""
# print("obj.author", obj.author)
# print("request", request.user)
obj_has_author = hasattr(obj, "author")
# print(obj_has_author)
if not obj_has_author:
obj.author = request.user
super().save_model(request, obj, form, change)
def get_form(self, request, obj=None, **kwargs):
if obj is None:
kwargs['form'] = ArticleAddForm
return super().get_form(request, obj, **kwargs)
class ArticleAddForm(forms.ModelForm):
class Meta:
model = Article
# fields = (' node', 'source', 'title', 'content', )
exclude = ['author', 'last_reply', 'is_comment', 'num_views', 'num_favorites']
widgets = {
"title": TextInput(attrs={"style": "width:45%;", "placeholder": "请输入标题"}),
}
class ArticleSupperAddForm(forms.ModelForm):
class Meta:
model = Article
exclude = ['author', 'last_reply', ]
widgets = {
"title": TextInput(attrs={"style": "width:45%;", "placeholder": "请输入标题"}),
}
# 主题表/文章表
class Article(models.Model):
title = models.CharField(max_length=128, verbose_name="标题")
content = RichTextUploadingField(verbose_name="内容", config_name='awesome_ckeditor')
node = models.ForeignKey(Node, verbose_name="所属节点")
author = models.ForeignKey(User, related_name="author_set", verbose_name="作者")
source = models.ForeignKey(Source, verbose_name="来源", blank=True,
null=True)
tags = models.ManyToManyField(Tag, verbose_name="标签", related_name="tags_set", blank=True)
num_views = models.IntegerField(default=0, verbose_name="浏览数量")
num_favorites = models.IntegerField(default=0, verbose_name="收藏数量")
last_reply = models.ForeignKey(User, related_name="last_reply_set", verbose_name="最后回复者", blank=True,
null=True)
time_created = models.DateTimeField(auto_now_add=True, verbose_name="发表时间")
time_updated = models.DateTimeField(blank=True, null=True, auto_now=True, verbose_name="更新时间")
show_status = models.BooleanField(default=True, verbose_name="是否显示")
is_comment = models.BooleanField(default=False, verbose_name="临时评论")
is_top = models.BooleanField(default=False, verbose_name="是否置顶")
class Meta:
verbose_name = "文章"
verbose_name_plural = "文章列表"
# 获取后台文本编辑器图文内容中图片url地址
def get_contentimg_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 get_absolute_url(self):
return reverse('blog:detail', kwargs={
"pk": self.id,
})
def get_full_url(self):
site = get_current_site().domain
url = "https://{site}{path}".format(site=site, path=self.get_absolute_url())
return url
def __str__(self):
if len(self.title) > 15:
title_short = self.title[:12] + '...'
else:
title_short = self.title
return "%s %s %s" % (self.id, self.author, title_short)