這篇文章帶給大家的內容是關於Django admin原始碼的分析(附範例),有一定的參考價值,有需要的朋友可以參考一下,希望對你有幫助。
單例模式(Singleton Pattern)是一種常用的軟體設計模式,該模式的主要目的是確保某一個類別只有一個實例存在。當你希望在整個系統中,某個類別只能出現一個實例時,單例物件就能派上用場。
例如,某個伺服器程式的設定資訊存放在一個檔案中,客戶端透過一個 AppConfig 的類別來讀取設定檔的資訊。如果在程式運行期間,有很多地方都需要使用設定檔的內容,也就是說很多地方都需要創建AppConfig 對象的實例,這就導致系統中存在多個AppConfig 的實例對象,而這樣會嚴重浪費記憶體資源,尤其是在設定檔內容很多的情況下。事實上,類似 AppConfig 這樣的類,我們希望在程式運行期間只存在一個實例物件。
在Python 中,我們可以用多種方法來實作單例模式:
#使用__new__()
__new__()
來控制實例的建立過程,程式碼如下:__new__()方法用來建立實例物件__init__()方法用來初始化實例物件class Person(object): def __init__(self, name, age): self.name = name self.age = age class Singleton(object): _instance = None def __new__(cls, *args, **kwargs): print(1) if not cls._instance: cls._instance = super(Singleton, cls).__new__(cls) return cls._instance def __init__(self, name, age): print(2) self.name = name self.age = age if __name__ == '__main__': p1 = Person("djb", 18) p2 = Person("djb", 18) print(p1 == p2) print(id(p1), id(p2)) print("=" * 120) s1 = Singleton("djb", 18) s2 = Singleton("djb", 18) print(s1 == s2) print(id(s1), id(s2))
class Singleton(object): def __init__(self, name, age): self.name = name self.age = age p1 = Singleton("djb", 18)
from singleton import p1 print(id(p1)) print(p1.name) p1.name = "Bob" from singleton import p1 print(id(p1)) print(p1.name)
def autodiscover(): autodiscover_modules('admin', register_to=site)
#admin.py class BookAdmin(admin.ModelAdmin): list_display = ("title",'publishDate', 'price') admin.site.register(Book, BookAdmin) admin.site.register(Publish)
admin.site.register(Book, BookAdmin) admin.site.register(Publish)
class ModelAdmin(BaseModelAdmin):pass def register(self, model_or_iterable, admin_class=None, **options): if not admin_class: admin_class = ModelAdmin # Instantiate the admin class to save in the registry self._registry[model] = admin_class(model, self)
urlpatterns = [ url(r'^admin/', admin.site.urls), ]
class AdminSite(object): def get_urls(self): from django.conf.urls import url, include urlpatterns = [] # Add in each model's views, and create a list of valid URLS for the # app_index valid_app_labels = [] for model, model_admin in self._registry.items(): urlpatterns += [ url(r'^%s/%s/' % (model._meta.app_label, model._meta.model_name), include(model_admin.urls)), ] if model._meta.app_label not in valid_app_labels: valid_app_labels.append(model._meta.app_label) return urlpatterns @property def urls(self): return self.get_urls(), 'admin', self.name
from django.shortcuts import HttpResponse def test01(request): return HttpResponse("test01") def test02(request): return HttpResponse("test02") urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^Xadmin/', ([ url(r'^test01/', test01), url(r'^test02/', test02), ],None,None)), ]
from django.conf.urls import url,include from django.contrib import admin from django.shortcuts import HttpResponse def change_list_view(request): return HttpResponse("change_list_view") def add_view(request): return HttpResponse("add_view") def delete_view(request): return HttpResponse("delete_view") def change_view(request): return HttpResponse("change_view") def get_urls(): temp=[ url(r"^$".format(app_name,model_name),change_list_view), url(r"^add/$".format(app_name,model_name),add_view), url(r"^\d+/del/$".format(app_name,model_name),delete_view), url(r"^\d+/change/$".format(app_name,model_name),change_view), ] return temp url_list=[] for model_class,obj in admin.site._registry.items(): model_name=model_class._meta.model_name app_name=model_class._meta.app_label # temp=url(r"{0}/{1}/".format(app_name,model_name),(get_urls(),None,None)) temp=url(r"{0}/{1}/".format(app_name,model_name),include(get_urls())) url_list.append(temp) urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^Xadmin/', (url_list,None,None)), ]
以上是Django admin原始碼的分析(附範例)的詳細內容。更多資訊請關注PHP中文網其他相關文章!