企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
>[success] # 将共同点抽象出来 ~~~ class BaseOwnerAdmin(admin.ModelAdmin): """ 1. 用来处理文章、分类、标签、侧边栏、友链这些model的owner字段自动补充 2. 用来针对queryset过滤当前用户的数据 """ exclude = ('owner', ) def get_queryset(self, request): qs = super(BaseOwnerAdmin, self).get_queryset(request) return qs.filter(owner=request.user) def save_model(self, request, obj, form, change): obj.owner = request.user return super(BaseOwnerAdmin, self).save_model(request, obj, form, change) ~~~ >[danger] ##### 使用 ~~~ from tbase.custom_admin import BaseOwnerAdmin # Register your models here. @admin.register(Post, site=custom_site) class PostAdmin(BaseOwnerAdmin): list_display = [ 'title', 'category', 'status', 'owner', 'created_time', 'operator' ] list_filter = ['category'] search_fields = ['title', 'category__name', 'owner__username'] # 编辑页面 save_on_top = True fields = ( ('category', 'title'), ('desc', 'status'), 'content', 'tag', ) def operator(self, obj): return format_html( '<a href="{}">编辑</a>', reverse('cus_admin:blong_post_change', args=(obj.id,)) ) operator.short_description = '操作' ~~~