Django常用Import模块


#用户相关
from django.contrib.auth import models
from django.contrib.auth import authenticate
from django.contrib.auth.models import Group,User

#Model,Admin,Form, Url中需引用
from django.conf import settings
from django.db import models
from django.contrib import admin
from django import forms
from django.urls import path

#View中引用
from django.shortcuts import render, HttpResponse, redirect
from django.http import JsonResponse

#数据相关操作
from django.db.models import Q
from django.db import connection
from django.core.cache import cache

#模板相关
from django.views.decorators.csrf import csrf_exempt
from django.template.defaultfilters import striptags

#其它
import django.utils.timezone as timezone
from django.utils.safestring import mark_safe

#异常
from django.core.exceptions import PermissionDenied
import traceback

#解析
import json
import base64
import html
import re

#时间
from datetime import datetime, timedelta
import time

#日志
import logging as log

AdminSample大全

def make_mobile(modeladmin, request, queryset):
    queryset.update(desktop=False)
make_mobile.short_description = "全端可见"

class SampleInline(admin.TabularInline):
    model = models.EchartReportSetup
    form = ReportSetupForm
    raw_id_fields = ('dataset','echartformat',)
    extra = 0 #默认显示条目的数量
    # show_change_link=True
    fieldsets=(['',{'fields':('dataset','div_format',)}],)

@admin.register(models.Sample)
class SampleAdmin(admin.ModelAdmin):
    list_per_page = 20
    readonly_fields = ('owner','report_link',)
    ordering = ('-project_name','name',)
    list_editable = ['datasets']  #可编辑的
    search_fields = ('name','remark')  #搜索字段
    raw_id_fields = ('html_file','theme_name','project_name')
    list_display_links = ('remark',)
    inlines = [SampleInline,]
    filter_horizontal = ('users','group')
    actions = ['make_mobile',]

    def get_inline_instances(self, request, obj=None):
        if request.GET.get('usr'):
            return []
        else:
            return super().get_inline_instances(request,obj)

    def get_list_filter(self, request):
        if request.user.user_permissions.values().filter(codename='change_sample') or\
                'echart.change_sample' in request.user.get_group_permissions():
            self.list_filter =('project_name','active',)
        else:
            self.list_filter =('project_name',)
        return self.list_filter

    def get_list_display_links(self, request, list_display):
        self.list_display_links = ('edit_link',)
        return self.list_display_links

    def get_list_display(self, request):
        self.change_list_template=None
        self.list_display = ( 'report_link','remark',)
        # self.change_list_template = ['admin/change_list_simple.html',]
        return self.list_display

    def get_actions(self, request):
        actions = super(SampleAdmin, self).get_actions(request)
        if not request.user.is_superuser:
            if 'delete_selected' in actions:
                del actions['delete_selected']
        # actions = None
        return actions

    def save_model(self, request, obj, form, change):
        if not request.user.is_superuser and obj.owner != request.user.username:
            self.message_user(request,'msg')
        obj.save()


    def get_queryset(self, request):
        qs = super(SampleAdmin,self).get_queryset(request)
        if request.user.is_superuser:
            return qs
        return qs.filter(Q(owner=request.user.username) |Q(users__exact=request.user.id)).ds_distinct()

    def get_fieldsets(self, request, obj=None):
        if request.GET.get('dev'):
            self.fieldsets=(['',{'fields':(('remark','theme_name'),'others',)}],)
        else:
            self.fieldsets = (
                    ['', {
                    'fields': (('name','remark',),)}],
                ['高级',{
                        'classes': ('collapse',),  # CSS
                        'fields': ('others',),
                    }]
            )
        return super(SampleAdmin,self).get_fieldsets(self, request)