About admin.
(1) Overview of admin.
admin is a django child component that is automatically registered in the INSTALLED_APPS section of the settings file when a project is created, and there is also a route for admin in the file
INSTALLED_APPS = [ #A component that comes with and is registered as an app '', '', '',
urlpatterns = [ # Auto-existing admin routes url(r'^admin/', ), url(r'^stark/', ), ]
(2) Execution flow of admin
When the django program is loaded, it automatically loads the files in the registered app in a loop and executes them.
# There is an __init__ file with the following code in it. # Meaning: the startup of the program automatically looks for a py file named admin, and then executes def autodiscover(): autodiscover_modules('admin', register_to=site)
# Specific methods are as follows. def autodiscover_modules(*args, **kwargs): """ Auto-discover INSTALLED_APPS modules and fail silently when not present. This forces an import on them to register any admin bits they may want. You may provide a register_to keyword parameter as a way to access a registry. This register_to object must have a _registry instance variable to access it. """
Contents of the implementation document
# At django startup, the system automatically loads the from import admin #Import the mods under this app from DRF import models # Here is a singleton pattern ()
singleton pattern site here is the application of a singleton pattern, for the AdminSite class of a singleton pattern, the implementation of each App in each one is an object
# AdminSite Class class AdminSite(object): ... def __init__(self, name='admin'): self._registry = {} # model_class class -> admin_class instance = name self._actions = {'delete_selected': actions.delete_selected} self._global_actions = self._actions.copy() all_sites.add(self) .... site = AdminSite()
Execute the register method
# register method in AdminSite def register(self, model_or_iterable, admin_class=None, **options): """ Registers the given model(s) with the given admin class. The model(s) should be Model classes, not instances. If an admin class isn't given, it will use ModelAdmin (the default admin options). If keyword arguments are given -- ., list_display -- they'll be applied as options to the admin class. If a model is already registered, this will raise AlreadyRegistered. If a model is abstract, this will raise ImproperlyConfigured. """
Knowledge Base: The Singleton Pattern
class AdminSite(object): def __init__(self): self._registry = {} obj1 = AdminSite()
import a a.obj1._registry['k2'] = 666
import a a.obj1._registry['k1'] = 123 print(a.obj1._registry)
Implementation of the Singleton Pattern
1: Use of modules
Python's modules are the natural singleton pattern.
Because the module generates a .pyc file when it is first imported, when it is imported a second time, the .pyc file is loaded directly without executing the module code again.
Therefore, we just need to define the relevant functions and data in a module to get a single instance object.
Example:
class V1(object): def foo(self) pass V1 = V1()
Save the above code in a file, to use, directly in other files to import the object in this file, this object is both the object of the singleton pattern
e.g. from a import V1
2: Using decorators
def Singleton(cls): _instance = {} def _singleton(*args, **kargs): if cls not in _instance: _instance[cls] = cls(*args, **kargs) return _instance[cls] return _singleton @Singleton class A(object): a = 1 def __init__(self, x=0): = x a1 = A(2) a2 = A(3)
3: Use class
4: Based on __new__ method implementation
When we instantiate an object, it is the __new__ method of the class that is executed first
When: (the default call to object.__new__ when we didn't write it), instantiate the object; then execute the __init__ method of the class to initialize this object, all we can implement the singleton pattern, based on this
This is the whole content of this article.