django admin Component Usage Detailed Explanation
- 2021-07-24 11:11:33
- OfStack
For admin:
(1) Overview of admin:
admin is a child component of django. When a project is created, it will be automatically registered in INSTALLED_APPS of settings file. In addition, the route of admin also exists in urls. py file
INSTALLED_APPS = [
# Brought-in and registered 1 Components are app
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
urlpatterns = [
# Automatically existing admin Route
url(r'^admin/', admin.site.urls),
url(r'^stark/', site.urls),
]
(2) Execution flow of admin
When the django program is loaded, the admin. py file in the registered APP is automatically looped and then executed
# In admin.py There are in the file 1 A __init__ Documents , The code is as follows
# Meaning : The startup of the program automatically looks for a program named admin Adj. py Documents , Then execute
def autodiscover():
autodiscover_modules('admin', register_to=site)
# The 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.
"""
Execute the contents of the admin. py file
# In django Startup time , Automatic loading of the system
from django.contrib import admin
# Import the APP Under models
from DRF import models
# Here is 1 Singleton pattern admion.site
admin.site.register(models.Publisher)
The singleton pattern site here is a singleton pattern. For a singleton pattern of AdminSite class, every admin. site in every App executed is an object
# AdminSite Class
class AdminSite(object):
...
def __init__(self, name='admin'):
self._registry = {} # model_class class -> admin_class instance
self.name = name
self._actions = {'delete_selected': actions.delete_selected}
self._global_actions = self._actions.copy()
all_sites.add(self)
....
site = AdminSite()
Execute the register method
# AdminSite In register Method
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 -- e.g., 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 supplement: singleton pattern
a.py
class AdminSite(object):
def __init__(self):
self._registry = {}
obj1 = AdminSite()
b.py
import a
a.obj1._registry['k2'] = 666
c.py
import a
a.obj1._registry['k1'] = 123
print(a.obj1._registry)
Implementation method of singleton pattern
1: Using modules
The modules of Python are natural singleton patterns.
Because the module generates the. pyc file when it is imported for the first time, the. pyc file is loaded directly when it is imported for the second time, and the module code is not executed again.
Therefore, we only need to define the related functions and data in a module, and we can get a singleton object.
For example:
urlpatterns = [
# Automatically existing admin Route
url(r'^admin/', admin.site.urls),
url(r'^stark/', site.urls),
]
0
Save the above code in the file test. py. When you want to use it, import the object in this file directly in other files. This object is a singleton object
For example: from a import V1
2: Use decorators
urlpatterns = [
# Automatically existing admin Route
url(r'^admin/', admin.site.urls),
url(r'^stark/', site.urls),
]
1
3: Using classes
4: Implementation based on __new__ method
When we instantiate an object, we first execute the __new__ method of the class
When: (we call object.__new__ by default when we don't write), instantiate the object; Then execute the __init__ method of the class to initialize this object, so we can implement the singleton pattern based on this