Django(admin)
一.DjangoAdmin
1.创建超级用户
python manage.py createsuperuser
2.登陆 浏览器中输入:
127.0.0.1:8000/admin
3.将表注册到admin中
-------------admin.py from app01 import models admin.site.register(models.Person) admin.site.register(models.Book)
4.更改djangoadmin中表的名字
class Clients(models.Model): #客户表 cli_teacher=models.CharField() #教师名字 cli_student=models.CharField() #学生名字 class Meta: verbose_name="客户表" verbose_name_plural="Client" #针对复数,表名为Clients,将在djangoadmin上显示Clientss,使用verbose_name_plural将在djangoadmin上显示Client
5.联合唯一索引
class Client(models.Model): #客户表 cli_teacher=models.CharField() #教师名字 cli_student=models.CharField() #学生名字 class Meta: unique_together=("cli_teacher","cli_student") #cli_teacher和cli_student都是不能重复的
6.将想要的字段添加到Admin中(用到1中的表)
ps:因为1中的__str__()方法只返回一个字段,想要更多字段 ------admin.py from app01 import models from django.contrib import admin class PeronAdmin(admin.ModelAdmin): list_display=("name") #字段名 class BookAdmin(admin.ModelAdmin): list_display=("b_id","b_name") admin.site.register(models.Person,PersonAdmin) admin.site.register(models.Book,BookAdmin)
如下图:
7.将字段设置为可编辑
class StudentAdmin(admin.ModelAdmin): list_display = ['id', 'attendance', 'score', 'student', ] list_editable = ['attendance', 'score', 'student', ] #此处有的字段list_display中必须有,而且必须有id admin.site.register(models.StudyRecord,StudentAdmin)