#准备好你的数据库模型思维导图
#先设计作者Author对象(表)[models.py]
class Author(models.Model): #继承于models.Model这个父类,从而实现多态
first_name=models.CharField(max_length=32) #名字的字段,使用字符串格式,最大长度32
last_name=models.CharField(max_length=32)
email=models.EmailField() #email字段,使用email自带的格式
def __unicode__(self): #定义unicode函数,是为了让对象在实例化的时候,可以返回打印输出它的名字<阿文>.不至于显示为<** object>
return "%s--%s"%(self.first_name,self.last_name)
#出版社
class Publisher(models.Model):
name=models.CharField(max_length=64,unique=True) #出版社名称,唯一,是主键
address=models.CharField(max_length=64,unique=True)
city=models.CharField(max_length=32)
state_province=models.CharField(max_length=32)
country=models.CharField(max_length=32)
website=models.URLField() #主页,采用自带的url格式
def __unicode__(self):
return "%s"%(self.name)
#定义一个选项,里面包含3个可选框,用以下面的书籍表publisher_state下拉选择
STATUS_CHOICES=(
('checkout',u'已出版'),
('dai',u'待出版'),
('status',u'审核中'),
)
#书籍表
class Book(models.Model):
name=models.CharField(max_length=64)
authors=models.ManyToManyField(Author) #作者,多对多的关系
publisher=models.ForeignKey(Publisher) #出版社,外键管理到Publisher表
publisher_date=models.DateField(auto_now_add=True)
publisher_state=models.CharField(max_length=20,choices=STATUS_CHOICES,default='checkout') #出版状态,是一个可选框
def __unicode__(self):
return "%s--%s"%(self.name,self.publisher_date)
python manage.py makemigrations
python manage.py migrate
##进入shell 模式 python manage.py shell ##导入Publisher对象 from book11.models import Publisher ##查询id=1的queryset赋值给p p=Publisher.objects.get(id=1) #对的对象进行操作,修改city="changsha",等价于[Publisher.objects.filter(id=1).update(city='changsha')]操作. p.city='changsha' #需要提交保存,否则不生效 p.save()
G:\git\web\books>python manage.py createsuperuser #增加一个超级用户 Username (leave blank to use 'huan5'): admin #用户名 Email address: admin@qq.com #邮箱 Password: #输8位密码2次 Password (again): Superuser created successfully.
#设置默认编码符 # -*- coding: utf-8 -*- from book11 import models #导入数据库 admin.site.register(models.Author) admin.site.register(models.Publisher) admin.site.register(models.Book)
#启动服务 python manage.py runserver 0.0.0.0:8086 #并打开浏览器执行 http://127.0.0.1:8086
from book11 import models
# 创建一个Bookadmin的modeladmin的子类
class Bookadmin(admin.ModelAdmin):
list_display=('id','name','publisher','publisher_date','publisher_state')
search_fields=('name',)
list_filter=('publisher','publisher_date',)
list_per_page=5
list_editable=('name','publisher_state',)
list_select_related=('publisher',)
filter_horizontal=('authors',)
raw_id_fields=('publisher',)
actions=['set_publisher_checkout','set_publisher_dai','set_publisher_status','set_publisher_del',]
admin.site.register(models.Author,Authoradmin) #不要忘了把这些定义好的扩展写进来 admin.site.register(models.Publisher,Publisheradmin) admin.site.register(models.Book,Bookadmin)
#配置作者页面的扩展内容
class Authoradmin(admin.ModelAdmin):
list_display=('first_name','last_name','email')
#配置出版社的扩展显示
class Publisheradmin(admin.ModelAdmin):
list_display = ('name','address','country',)
def set_publisher_checkout(modeladmin,request,queryset):
selected=request.POST.getlist(admin.ACTION_CHECKBOX_NAME) #选中传入的表单中,勾选的checkbox对应的id集合
models.Book.objects.filter(id__in=selected).update(publisher_state='checkout') #将所有选中的id对象,修改出版状态为checkout
def set_publisher_dai(modeladmin,request,queryset):
selected=request.POST.getlist(admin.ACTION_CHECKBOX_NAME)
models.Book.objects.filter(id__in=selected).update(publisher_state='dai')
def set_publisher_status(modeladmin,request,queryset):
selected=request.POST.getlist(admin.ACTION_CHECKBOX_NAME)
models.Book.objects.filter(id__in=selected).update(publisher_state='status')
def set_publisher_del(modeladmin,request,queryset): #########扩展部分,增加对选中的记录今夕删除!###########
selected=request.POST.getlist(admin.ACTION_CHECKBOX_NAME)
models.Book.objects.filter(id__in=selected).delete()
set_publisher_checkout.short_description="设置所有的书籍为--已出版" #为了使界面更加友好,添加别名
set_publisher_status.short_description="设置所有的书籍为--审核中"
set_publisher_dai.short_description="设置所有的书籍为--待出版"
set_publisher_del.short_description="设置所有的书籍为--删除"
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.contrib import admin
from book11 import models
# Register your models here.
class Bookadmin(admin.ModelAdmin):
list_display=('id','name','publisher','publisher_date','publisher_state')
search_fields=('name',)
list_filter=('publisher','publisher_date',)
list_per_page=5
list_editable=('name','publisher_state',)
list_select_related=('publisher',)
filter_horizontal=('authors',)
raw_id_fields=('publisher',)
actions=['set_publisher_checkout','set_publisher_dai','set_publisher_status','set_publisher_del',]
def set_publisher_checkout(modeladmin,request,queryset):
selected=request.POST.getlist(admin.ACTION_CHECKBOX_NAME)
models.Book.objects.filter(id__in=selected).update(publisher_state='checkout')
def set_publisher_dai(modeladmin,request,queryset):
selected=request.POST.getlist(admin.ACTION_CHECKBOX_NAME)
models.Book.objects.filter(id__in=selected).update(publisher_state='dai')
def set_publisher_status(modeladmin,request,queryset):
selected=request.POST.getlist(admin.ACTION_CHECKBOX_NAME)
models.Book.objects.filter(id__in=selected).update(publisher_state='status')
def set_publisher_del(modeladmin,request,queryset):
selected=request.POST.getlist(admin.ACTION_CHECKBOX_NAME)
models.Book.objects.filter(id__in=selected).delete()
set_publisher_checkout.short_description="设置所有的书籍为--已出版"
set_publisher_status.short_description="设置所有的书籍为--审核中"
set_publisher_dai.short_description="设置所有的书籍为--待出版"
set_publisher_del.short_description="设置所有的书籍为--删除"
class Authoradmin(admin.ModelAdmin):
list_display=('first_name','last_name','email')
class Publisheradmin(admin.ModelAdmin):
list_display = ('name','address','country',)
admin.site.register(models.Author,Authoradmin)
admin.site.register(models.Publisher,Publisheradmin)
admin.site.register(models.Book,Bookadmin)
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有