Djangoadmin相关知识
Posted xiangtingshen
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Djangoadmin相关知识相关的知识,希望对你有一定的参考价值。
https://www.cnblogs.com/yuanchenqi/articles/6083427.htm
https://www.cnblogs.com/haiyan123/p/8034430.html
Django admin :关于数据库的一个后台管理工具
admin是django强大功能之一,它能共从数据库中读取数据,呈现在页面中,进行管理。默认情况下,它的功能已经非常强大,如果你不需要复杂的功能,
它已经够用,但是有时候,一些特殊的功能还需要定制,比如搜索功能,下面这一系列文章就逐步深入介绍如何定制适合自己的admin应用。
如果你觉得英文界面不好用,可以在setting.py 文件中修改以下选项
LANGUAGE_CODE = ‘en-us‘ #LANGUAGE_CODE = ‘zh-hans‘
一 认识ModelAdmin
管理界面的定制类,如需扩展特定的model界面需从该类继承。
二 注册medel类到admin的两种方式:
<1> 使用register的方法
admin.site.register(Book,MyAdmin)
<2> 使用register的装饰器
@admin.register(Book)
三 掌握一些常用的设置技巧
- list_display: 指定要显示的字段
- search_fields: 指定搜索的字段
- list_filter: 指定列表过滤器
- ordering: 指定排序字段
from django.contrib import admin from app01.models import * # Register your models here. # @admin.register(Book)#----->单给某个表加一个定制 class MyAdmin(admin.ModelAdmin): list_display = ("title","price","publisher") search_fields = ("title","publisher") list_filter = ("publisher",) ordering = ("price",) fieldsets =[ (None, {‘fields‘: [‘title‘]}), (‘price information‘, {‘fields‘: [‘price‘,"publisher"], ‘classes‘: [‘collapse‘]}), ] admin.site.register(Book,MyAdmin) admin.site.register(Publish) admin.site.register(Author)
创建超级管理员
python manage.py createsuperuser
登陆admin
from django.db import models # Create your models here. class Book(models.Model): name = models.CharField(max_length=32) price = models.IntegerField() pub_date = models.DateField() publish = models.ForeignKey("Publish",on_delete=models.CASCADE) authors = models.ManyToManyField("Author") def __str__(self): return self.name class Publish(models.Model): name = models.CharField(max_length=32) city = models.CharField(max_length=32) def __str__(self): return self.name class Author(models.Model): name = models.CharField(max_length=32) age = models.IntegerField() def __str__(self): return self.name # class Book_Author(models.Model): # book = models.ForeignKey("Book", on_delete=models.CASCADE) # author = models.ForeignKey("Author", on_delete=models.CASCADE)
admin.py
from django.contrib import admin from app import models # Register your models here. admin.site.register(models.Book) admin.site.register(models.Publish) admin.site.register(models.Author)
自定义admin样式
admin.py
from django.contrib import admin from app import models # Register your models here. class BookAdmin(admin.ModelAdmin): list_display = (‘name‘,‘price‘,‘pub_date‘) admin.site.register(models.Book,BookAdmin) admin.site.register(models.Publish) admin.site.register(models.Author)
注意:list_display不可以加多对多的关系
此时price,pub_date不可以直接在页面上edit,需要点击书名进入修改页面,若想直接修改,需要再加一个字段
class BookAdmin(admin.ModelAdmin): list_display = (‘name‘,‘price‘,‘pub_date‘) list_editable = (‘price‘,‘pub_date‘)
增加字段选择author, filter_horizontal
class BookAdmin(admin.ModelAdmin): list_display = (‘name‘,‘price‘,‘pub_date‘) list_editable = (‘price‘,‘pub_date‘) filter_horizontal = (‘authors‘,)
list_per_page
class BookAdmin(admin.ModelAdmin): list_display = (‘name‘,‘price‘,‘pub_date‘) list_editable = (‘price‘,‘pub_date‘) filter_horizontal = (‘authors‘,) list_per_page = 2
search_fields
class BookAdmin(admin.ModelAdmin): list_display = (‘name‘,‘price‘,‘pub_date‘) list_editable = (‘price‘,‘pub_date‘) filter_horizontal = (‘authors‘,) list_per_page = 2 search_fields = (‘name‘,‘id‘,‘publish__name‘)
list_filter
class BookAdmin(admin.ModelAdmin): list_display = (‘name‘,‘price‘,‘pub_date‘) list_editable = (‘price‘,‘pub_date‘) filter_horizontal = (‘authors‘,) list_per_page = 2 search_fields = (‘name‘,‘id‘,‘publish__name‘) list_filter = (‘pub_date‘,‘publish‘)
verbose_name
class Book(models.Model): name = models.CharField(max_length=32,verbose_name="姓名") price = models.IntegerField("价格") pub_date = models.DateField() publish = models.ForeignKey("Publish",on_delete=models.CASCADE) authors = models.ManyToManyField("Author")
ording
class BookAdmin(admin.ModelAdmin): list_display = (‘name‘,‘price‘,‘pub_date‘) list_editable = (‘price‘,‘pub_date‘) filter_horizontal = (‘authors‘,) # list_per_page = 2 search_fields = (‘name‘,‘id‘,‘publish__name‘) list_filter = (‘pub_date‘,‘publish‘) ordering = (‘-price‘,) # 加"-"倒序
以上是关于Djangoadmin相关知识的主要内容,如果未能解决你的问题,请参考以下文章
Django学习第28篇:Django Admin的相关知识
spring练习,在Eclipse搭建的Spring开发环境中,使用set注入方式,实现对象的依赖关系,通过ClassPathXmlApplicationContext实体类获取Bean对象(代码片段