django_admin详解(2)
1.关于Admin的列表的基本设置
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| class NotesAdmin(admin.ModelAdmin): list_display = ('title', 'type', 'note_author', 'key_word', 'praise', 'read_counts', 'status', 'publish_date') ordering = ('-type',) actions = ['make_published', 'make_draft', 'confirm_author'] actions_on_top = True list_per_page = 20 list_editable = ('type',) search_fields = ('type',)
|
2.筛选过滤器:
1
| list_filter = ('title', 'type', 'key_word', 'status')
|
3.为不同字段结果添加不同的颜色
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| def colored_status(self): global color_code if self.status == 'p': color_code = 'green' elif self.status == 'd': color_code = 'red' return format_html( '<span style="color:{};font-size:20px;font-weight:bolder;">{}</span>', color_code, self.status ) colored_status.short_description = '文章状态'
|
4.根据不同的用户过滤不同的数据
1 2 3 4 5 6 7
| def get_queryset(self, request): all_result = super().get_queryset(request) if request.user.is_superuser: return all_result else: return all_result.filter(ips_author=request.user)
|
5.删除一些动作权限
1 2 3 4 5 6 7 8 9 10
| def get_actions(self, request): actions = super().get_actions(request) if not request.user.is_superuser: if 'can delete information' in actions: del actions['can delete information'] if 'can add information' in actions: del actions['can add information'] return actions
|