Five. Django own use admin page, singleton appreciated custom admin tool assembly Django-admin management object-oriented singleton

A. Django own use admin page

https://www.cnblogs.com/yuanchenqi/articles/8323452.html          Django-ADMIN management tools

https://www.cnblogs.com/lovershowtime/p/11353293.html            ADMIN management background

Django provides a web-based management tools. 

Django automated management tools are part of django.contrib. You can INSTALLED_APPS in settings.py the project see it: 

# the Application Definition 

INSTALLED_APPS = [
     ' django.contrib.admin ' ,
     ' django.contrib.auth ' ,
     ' django.contrib.contenttypes ' ,
     ' django.contrib. Sessions ' ,
     ' django.contrib.messages ' ,
     ' django.contrib.staticfiles ' ,
     " app01 " 
]

 II. Singleton pattern (admin to use the singleton) 

https://www.cnblogs.com/Sup-to/p/11094767.html      object-oriented singleton

Singleton (Singleton Pattern) is a common software design pattern, the main purpose of this mode is to ensure that only one instance of a particular class exists . When you want in the whole system, can appear only one instance of a class, singleton objects can come in handy.
Singleton: based on a method to obtain multiple instances of the same instance is 
when the object is instantiated multiple times obtained properties are stored in the same case, a plurality of objects should point to the same memory, i.e., the same instance 
reduce the memory occupancy
# 单例模式__new__ 方法一

class Singleton(object):
    def __new__(cls, *args, **kwargs):
        if not hasattr(cls,'_instance'):
            orig=super(Singleton,cls)
            cls._instance=orig.__new__(cls,*args,**kwargs)
        return cls._instance

class MyClass(Singleton):
    a=1
one=MyClass()
two=MyClass()
one.a=20
two.a=30
#one and two are identical, can be used id (), ==, is checked 
Print (one.a)     #
 Print (two.a)
 Print (ID (one))   #
 Print (ID (two))   #
 Print (= one TWO =)    # True 
Print (One IS TWO)    # True 


# 30 
# 30 
# 2418859210792 
# 2418859210792 
# True 
# True
# 单例模式装饰器 方法二
def singleton(cls, *args, **kwargs):
    instances = {}
    def _singleton():
        if cls not in instances:
            instances[cls] = cls(*args, **kwargs)
        return instances[cls]
    return _singleton
@singleton
class MyClass3(object):
    a = 1
one = MyClass3()
two = MyClass3()
print(id(one))  # 2880466769232
print(id(two))  # 2880466769232
print(one == two)  # True
print(one is two)  # True

 Use module (singleton)

In fact, Python module is a natural Singleton pattern, because the module at the time of the first import, will generate .pyc files when importing the second time, it will load .pyc files directly, 
without execution module code again. Thus, we only need to define the data and related functions in one module, it is possible to obtain a single embodiment of the subject. If we really want a singleton class, you can consider doing so
sing.py 

class Sun (Object):   # before you export will generate a temporary file packaged pyc past 
    AA = 100 DEF RUN (Self):
         Print (self.aa) 
SS = Sun ()
 Print ( " the ok !!! " ) Print ( " rrrrrrrrrrrrrrrrrrrrrrrrrr " )
    

main.py 

# guide to a file loaded only once to a temporary file pyc in the value of imported second time did not have any sense born singleton 

from   Sing Import   SS 
ss.run () 
Print (the above mentioned id (ss.run ()) ) 


# from Sing Import SS 
ss.run ()
 Print (the above mentioned id (ss.run ())) 


# the ok! ! ! 
# Rrrrrrrrrrrrrrrrrrrrrrrrrr 
# 100 
# 100 
# 1720755408 
# 100 
# 100 
# 1720755408


sing.py
class Sun (Object): AA = 100 DEF RUN (Self): Print (self.aa) SS = Sun () Print ( " the ok !!! " ) Print ( " rrrrrrrrrrrrrrrrrrrrrrrrrr " ) # will produce before exporting pyc a temporary file package in the past
fun.py 


from Sing Import   ss   # This is ss Sun class instance object 

DEF foo ():
     Print (ID (ss), " ok11111111 " )
main.py 

# guide to a file only once to load the value pyc second temporary file import did not have any sense born singleton 

from Sing Import   ss   # this is ss Sun instance of an object class 
Print (the above mentioned id (ss) ) 


from Sing import   ss    # this is ss Sun instance of an object class 
Print (the above mentioned id (ss)) 


from   Fun import * 
foo () 

# these three documents are a part of this program as long as a program to import all the other places he is a Object 

# the ok! ! ! 
# Rrrrrrrrrrrrrrrrrrrrrrrrrr 
# 2241649670520 
# 2241649670520 
# 2241649670520 ok11111111

 

sing.py 

class Sun (Object): 
    AA = 100
     DEF RUN (Self):
         Print (self.aa) 
SS = Sun ()
 Print ( " the ok !!! " )
 Print ( " rrrrrrrrrrrrrrrrrrrrrrrrrr " ) 

# will produce before exporting pyc a temporary file package in the past
func.py


from sing import  ss

def foo():
    print (id(ss),"ok11111111")
the main.py 

# guiding a document to only the second load values did not have any meaning introducing natural single embodiment pyc once to the temporary file 

from Sing Import   SS, the Sun #   the Sun class object 
Print (ID (SS)) 


A = the Sun ()
 Print (ID (A)) 


B = the Sun ()
 Print (ID (B)) 

# 
# OK! ! ! 
# Rrrrrrrrrrrrrrrrrrrrrrrrrr 
# 1267286899120 
# 1,267,285,115,232 
# 1,267,286,898,784

 III. Components custom admin

admin execution flow [ Print (admin.site._registry) # execution results ]

Admin.py perform cyclic loading all files registered in the app 

DEF Autodiscover (): 
    autodiscover_modules ( ' ADMIN ' , register_to = Site)
 <2> execution code 


# admin.py 

class BookAdmin (admin.ModelAdmin): 
    list_display = ( " title " , ' publishDate ' , ' . price ' ) 

admin.site.register (Book, BookAdmin) 
admin.site.register (the Publish)

admin.site  

 
Here the application is a single-mode embodiment, for a singleton class AdminSite each app executed each admin.site is an object
 
execution register method

admin.site.register (Book, BookAdmin)
admin.site .register (Publish)


classModelAdmin (BaseModelAdmin):Pass

DEFthe Register (Self, model_or_iterable, admin_class = None, **Options):
    IF notadmin_class:
            admin_class=ModelAdmin
    #Instantiate at The ADMIN class to the Save in at The Registry
    self._registry [Model ] =admin_class (Model, Self)


thinking: plus in every app in the admin .py

 Print (admin.site._registry) # execution results? 
Here, registration closes! 

 admin's URL configuration

urlpatterns= [
    url(r'^admin/', admin.site.urls),
]

  I. extended application url () method (the original url design)

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'^yuan/', ([
                    url(r'^test01/', test01),
                    url(r'^test02/', test02),

                    ],None,None)),

]

 

Guess you like

Origin www.cnblogs.com/lovershowtime/p/11588471.html