13 - stark总结、github代码
1、stark - 总结
(单例,继承,反射,面向对象,modelform 应用得很好!!)
1.注册表
单例模式 site = StarkSite()
2.生成url
url(r'^stark/', ([],None,None))
3.数据列表展示
可自定义配置显示:
list_display = ["__str__"]
list_display_links = []
modelform_class = []
search_fields = []
actions = []
list_filter = []
4.增删改页面 modelform
5.分页
自定义分页组件 stark/utils/page.py
class Pagination(object):
...
...
6.search模糊查询
Q查询 or
search_connection = Q()
...
data_list = self.model.objects.all().filter(search_connection)
7.action批量处理
def patch_init(self, request, queryset):
queryset.update(price=123)
...
patch_init.short_description = "批量初始化"
actions = [patch_init]
queryset = self.model.objects.filter(pk__in=selected_pk)
8.filter过滤
list_filter = ['title','publish', 'authors']
eg:{"publish":["<a href=''>全部</a>","<a href=''>南京出版社</a>","<a href=''>上海出版社</a>"]
"authors":["<a href=''>全部</a>","<a href=''>yuan</a>","<a href=''>egon</a>"]
}
Q查询 and
filter_condition = Q()
data_list = self.model.objects.all().filter(search_connection).filter(filter_condition)
9.pop弹出
在一对多和多对多字段后渲染 +
+对应的跳转路径
保存添加记录同时,将原页面的对应的下拉菜单中添加该记录
2、各种小知识点
from django.test import TestCase # Create your tests here. # # class A(object): # # x=12 # def __init__(self,m): # self.z = m # def xxx(self): # print(self.x) # print(self.z) # # class B(A): # x=5 # z = 11 # # b=B(10) # b.xxx() ####################################### # # class Person(object): # def __init__(self,name): # self.name = name # # alex = Person('alex') # print(alex.name) # # s = 'name' # # # print(alex.s) # 用反射 # # getattr(alex,s) # # print(getattr(alex,s)) ####################################### # 没学面向对象之前,都是函数 , # 函数 方法 # class Person(object): # def __init__(self,name): # self.name = name # # def eat(self): # 方法! # print(self) # print('eating...') # # # 实例方法 # # egon = Person('egon') # # egon.eat() # # # 函数 # Person.eat('ss') ####################################### # list = [1,2,3] # list.append(4) # print(list) # list.insert(0,100) # print(list) ####################################### # li = [] # print(len(li)) # # s = "sss" # print(isinstance(s,str)) ####################################### # class Person(object): # def __init__(self,name): # self.name = name # # def __str__(self): # return self.name # # alex = Person('alex') # # print(alex.name) # # print(alex) # # print(alex.__str__()) # print(str(alex)) # # print(getattr(alex,'__str__')()) ####################################### # temp = [] # temp.append(1) # temp.extend([1,2,3]) # print(temp) ####################################### # def foo(): # return # # print(foo.__name__) ####################################### # 查询是字段名称 # Book.objects.filter(Q(title='yuan')|Q(price='123')) # Q() 查询放str, # q = Q() # q.connection = 'or' # q.children.append(('title','yuan')) # q.children.append(('price',123)) ####################################### # ret = self.model.objects.filter(title__startswith='py') # ret = self.model.objects.filter(price__in=[123, 111, 21, 11]) # ret = self.model.objects.filter(price__range=[10, 100]) # ret = self.model.objects.filter(title__contains='y') # ret = self.model.objects.filter(title__contains='o') # ret = self.model.objects.filter(title__icontains='o') # print(ret) # return HttpResponse('ok') ####################################### # def foo(): # print('ok') # # print(foo.__name__) # print(type(foo.__name__)) # foo.desc = '123' # print(foo.desc) # a = foo() # a.desc = 12 # print(a.desc) ####################################### # class A(): # str = "http://127.0.0.1:8000/stark/app01/book/?publish=1&author=5" ####################################### # class A(object): # pass # # class B(A): # pass # # b = B() # print(isinstance(b,A)) # True # print(isinstance(b,B)) # True test.py
3、目录结构
4、github代码
github代码 : https://github.com/venicid/stark-Xadmin
xadmin-easy https://github.com/venicid/Xadmin-easy
原始版
https://github.com/alice-bj/stark_pro_0
简洁版
https://github.com/alice-bj/stark_admin