1 提交根评论 2 显示根评论 --- render显示 --- ajax显示 3 提交子评论 4 显示子评论 ---- render显示 ---- Ajax显示(扩展) 5 评论树 博客:富文本编辑器 beautfulSoup模块 ret=Book.objects.all().exist() # sql; limit 1 if ret: print("OK") Book.objects.all().distinct() # XXXX select distinct(age) from Book Book.objects.all().values("title").distinct()
sql: 单表处理: Book id title price publish email addr 1 python 100 苹果出版社 123@qq.com 北京 2 go 200 橘子出版社 234@qq.com 南京 3 java 211 橘子出版社 234@qq.com 南京 4 linux 22 橘子出版社 234@qq.com 南京 ###################一对多######################多表: Book id title price publish_id 1 python 100 1 2 go 200 2 3 java 211 2 4 linux 22 2 Publish id name email addr 1 苹果出版社 123@qq.com 北京 2 橘子出版社 234@qq.com 南京 结论: 一旦确定表关系是一对多,那么关联字段建在多的表中 book: Foreignkey publish_id refference Publish(id) #######################多对多################################# Book id title price publish_id 1 python 100 1 2 go 200 2 3 java 211 2 4 linux 22 2 Author id name age 1 alex 38 2 liwen 43 Author_Book id author_id book_id 1 1 1 2 1 2 3 2 2 结论;一旦是多对多的关系:需要建立第三张关系表 #######################一对一################################# Author id name age 1 alex 38 2 liwen 43 AuthorDetail id tel gf author_id(unique) 1 131 钢蛋 1 2 121 铁锤 2 SELECT "app01_book"."title" FROM "app01_publish" LEFT OUTER JOIN "app01_book" ON ("app01_publish"."nid" = "app01_book"."publish_id") WHERE "app01_publish"."name" = 苹果出版社 SELECT "app01_book"."title" FROM "app01_book" INNER JOIN "app01_publish" ON ("app01_book"."publish_id" = "app01_publish"."nid") WHERE "app01_publish"."name" = 苹果出版社 SELECT "app01_book"."title", "app01_publish"."name" FROM "app01_book" INNER JOIN "app01_book_authors" ON ("app01_book"."nid" = "app01_book_authors"."book_id") INNER JOIN "app01_author" ON ("app01_book_authors"."author_id" = "app01_author"."nid") INNER JOIN "app01_authordetail" ON ("app01_author"."authorDetail_id" = "app01_authordetail"."nid") INNER JOIN "app01_publish" ON ("app01_book"."publish_id" = "app01_publish"."nid") WHERE "app01_authordetail"."telephone" LIKE 151% ESCAPE '\'
sql: 单表处理: Book id title price publish email addr 1 python 100 苹果出版社 123@qq.com 北京 2 go 200 橘子出版社 234@qq.com 南京 3 java 211 橘子出版社 234@qq.com 南京 4 linux 22 橘子出版社 234@qq.com 南京 ###################一对多######################多表: Book id title price publish_id 1 python 100 1 2 go 200 2 3 java 211 2 4 linux 22 2 Publish id name email addr 1 苹果出版社 123@qq.com 北京 2 橘子出版社 234@qq.com 南京 结论: 一旦确定表关系是一对多,那么关联字段建在多的表中 book: Foreignkey publish_id refference Publish(id) #######################多对多################################# Book id title price publish_id 1 python 100 1 2 go 200 2 3 java 211 2 4 linux 22 2 1 聚合与分组 # 统计不止一个作者的图书名称: select book.title,Count(author.name) as c from book inner join book-authors on ... inner join author on ... group by book.id having c>1 2 admin配置参数 后台数据库管理页面 admin model: 对每一个app下的model设计出增伤改查4个url 查看book: http://127.0.0.1:8000/admin/app01/book/ 添加book: http://127.0.0.1:8000/admin/app01/book/add/ 编辑book: http://127.0.0.1:8000/admin/app01/book/1/change/ 删除book: http://127.0.0.1:8000/admin/app01/book/1/delete/ 注册: from django.contrib import admin # Register your models here. from .models import * from django.utils.safestring import mark_safe class BookConfig(admin.ModelAdmin): def deletes(self): return mark_safe("<a href=''>删除</a>") list_display = ["title","price","publishDate",'publish',deletes] list_display_links = ["price"] list_filter=["price","title","authors","publish"] list_editable=["title",] search_fields=["title","price"] def patch_init(self,request,queryset): queryset.update(price=100) patch_init.short_description = "批量初始化" actions = [patch_init,] change_list_template="list.html" fields = ('title',) admin.site.register(Book,BookConfig) admin.site.register(Publish) admin.site.register(Author) admin.site.register(AuthorDetail) Admin的实现流程: 1 启动: import admin def autodiscover(): autodiscover_modules('admin', register_to=site) 执行每一个app下的admin.py文件 2 注册 单例模式 3 设计URL Xadmin组件 3 ModelForm组件 CRM 1 Xadmin组件 2 权限 3 crm ''' select Max(book.price) as max_price from author inner join book_authors on ... inner join join book on ... group by author.name ''' Author id name age 1 alex 38 2 liwen 43 Author_Book id author_id book_id 1 1 1 2 1 2 3 2 2 结论;一旦是多对多的关系:需要建立第三张关系表 #######################一对一################################# Author id name age 1 alex 38 2 liwen 43 AuthorDetail id tel gf author_id(unique) 1 131 钢蛋 1 2 121 铁锤 2 SELECT "app01_book"."title" FROM "app01_publish" LEFT OUTER JOIN "app01_book" ON ("app01_publish"."nid" = "app01_book"."publish_id") WHERE "app01_publish"."name" = 苹果出版社 SELECT "app01_book"."title" FROM "app01_book" INNER JOIN "app01_publish" ON ("app01_book"."publish_id" = "app01_publish"."nid") WHERE "app01_publish"."name" = 苹果出版社 SELECT "app01_book"."title", "app01_publish"."name" FROM "app01_book" INNER JOIN "app01_book_authors" ON ("app01_book"."nid" = "app01_book_authors"."book_id") INNER JOIN "app01_author" ON ("app01_book_authors"."author_id" = "app01_author"."nid") INNER JOIN "app01_authordetail" ON ("app01_author"."authorDetail_id" = "app01_authordetail"."nid") INNER JOIN "app01_publish" ON ("app01_book"."publish_id" = "app01_publish"."nid") WHERE "app01_authordetail"."telephone" LIKE 151% ESCAPE '\'
1 ORM分组查询 2 admin 参数配置 3 admin流程 (1) 启动 autodiscover_modules('admin', register_to=site) (2) 注册 单例模式 admin.site=AdminSite(): 单例对象 class AdminSite(object): def __init__(self, name='admin'): self._registry = {} def register(self, model, admin_class=None, **options): if not admin_class: admin_class = ModelAdmin self._registry[model] = admin_class(model, self) # {Book:ModelAdmin(Book)} admin.site.register(Book) # admin.site._registry={Book:ModelAdmin(Book)} admin.site.register(Publish) # admin.site._registry={Book:ModelAdmin(Book),Publish:ModelAdmin(Publish)} class Authoconfig(admin.ModelAdmin): pass admin.site.register(Author,Authoconfig) # admin.site._registry={Book:ModelAdmin(Book),Publish:ModelAdmin(Publish),Author:Authoconfig(Author)} (3) 设计URL 为每一个app下的model设计增删改查4个url,以book为例: 127.0.01:8000/admin/app01/book/ 127.0.01:8000/admin/app01/book/add 127.0.01:8000/admin/app01/book/1/change/ 127.0.01:8000/admin/app01/book/1/delete/ url.py: [ url(r'^index/', index), # index(reqeust) ] { <class 'django.contrib.auth.models.Group'>: <django.contrib.auth.admin.GroupAdmin object at 0x000000000B5CA5F8>, <class 'django.contrib.auth.models.User'>: <django.contrib.auth.admin.UserAdmin object at 0x000000000B5F9080>, <class 'app01.models.Book'>: <app01.admin.BookConfig object at 0x000000000B601D68> } { <class 'django.contrib.auth.models.Group'>: <django.contrib.auth.admin.GroupAdmin object at 0x000000000B5CA5F8>, <class 'django.contrib.auth.models.User'>: <django.contrib.auth.admin.UserAdmin object at 0x000000000B5F9080>, <class 'app01.models.Book'>: <app01.admin.BookConfig object at 0x000000000B601D68>, <class 'app01.models.Publish'>: <django.contrib.admin.options.ModelAdmin object at 0x000000000B601D30>, <class 'app01.models.Author'>: <django.contrib.admin.options.ModelAdmin object at 0x000000000B601DD8>, <class 'app01.models.AuthorDetail'>: <django.contrib.admin.options.ModelAdmin object at 0x000000000B601E10> } HTTP: 什么是url: 协议://IP:8000/路径?a=1&b=2 http://127.0.0.1:8000/index/car/3?a=1 url("^yuan/",([ url(r'^test01/', ([ url(r'^test04/', test04), url(r'^test05/', test05), ],None,None)), url(r'^test02/', test02), url(r'^test03/', test03), ],None,None)) 需求: 为每一个app下的model设计增删改查4个url 127.0.01:8000/Xadmin/app01/book/ 127.0.01:8000/Xadmin/app01/book/add 127.0.01:8000/Xadmin/app01/book/1/change/ 127.0.01:8000/Xadmin/app01/book/1/delete/
原生form forms组件 ChoiceField(Field) ModelChoiceField(ChoiceField) ModelMultipleChoiceField(ModelChoiceField) 1 针对form表单设计form组件 modelform class Book(models.Model): title=models.CharField(max_length=32) price=models.DecimalField(max_digits=8,decimal_places=2) # 999999.99 date=models.DateField() publish=models.ForeignKey("Publish") authors=models.ManyToManyField("Author") class BookForm(forms.Form): title = forms.CharField(max_length=32,label="书籍名称") price = forms.DecimalField(max_digits=8, decimal_places=2,label="价格") # 999999.99 date = forms.DateField(label="日期", widget=widgets.TextInput(attrs={"type":"date"}) ) #gender=forms.ChoiceField(choices=((1,"男"),(2,"女"),(3,"其他"))) #publish=forms.ChoiceField(choices=Publish.objects.all().values_list("pk","title")) publish=forms.ModelChoiceField(queryset=Publish.objects.all()) authors=forms.ModelMultipleChoiceField(queryset=Author.objects.all())