1. 程式人生 > >13 - stark總結、github代碼

13 - stark總結、github代碼

bubuko 同時 condition temp 過濾 title lin app 小知識

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

原始版

  https://github.com/alice-bj/stark_pro_0

簡潔版

  https://github.com/alice-bj/stark_admin

13 - stark總結、github代碼