python-admin管理后台

什么是admin管理后台

配置步骤


image
image
image
image

添加组

image

增加用户

image
image
image
image

对自定义表进行操作

注册自定义模型类

image
bookstore/admin.py

from django.contrib import admin
from .models import Book
# Register your models here.
# 
admin.site.register(Book)

![image](https://img2020.cnblogs.com/blog/1847830/202108/1847830-20210813091100380-
1343933388.png)

显示样式与__str__相同

image

模型管理器类

image
image
image

list_display

from django.contrib import admin
from .models import Book
# Register your models here.
# 

class BookMaage(admin.ModelAdmin):
    list_display=['id','title','pub','price']


admin.site.register(Book,BookMaage)

image

显示列名参数verbose_name

image

list_filter添加过滤器

from django.contrib import admin
from .models import Book
# Register your models here.
class BookMaage(admin.ModelAdmin):
    list_display=['id','title','pub','price']
   #过滤器 分类查询
    list_filter=['pub']
admin.site.register(Book,BookMaage)

image

search_fields


from django.contrib import admin
from .models import Book
# Register your models here.
# 

class BookMaage(admin.ModelAdmin):
    list_display=['id','title','pub','price']
    #添加搜索框[模糊搜索]
    search_fields=['title']

admin.site.register(Book,BookMaage)

image

添加可编辑字段 list_editable

from django.contrib import admin
from .models import Book
# Register your models here.
# 

class BookMaage(admin.ModelAdmin):
    list_display=['id','title','pub','price']
    
    #控制 list_display中的字段,可以链接到修改页 ,必须是list_display中
    list_display_links=['title']

    #过滤器 分类查询
    list_filter=['pub']

    #添加搜索框[模糊搜索]
    search_fields=['title']

    #添加可编辑字段
    list_editable=['price']

admin.site.register(Book,BookMaage)

image

posted @ 2021-08-15 15:22  yescarf  阅读(960)  评论(0编辑  收藏  举报