python seventy-second Tian, admin source code analysis

django settings source code analysis

Settingso module introduced into the source, will find settings of an object is a single instance of the class of the embodiment LazySettings generated
will go function after its internal _setup LazySettings instantiated


importlib action module: module introduced in the form of a string, to get access to the module namespace name
module = imporlib.import_module ( 'string')


settings configure user-defined principle:
When the project starts, it will use os.environ.settdefault ( 'xxx', 'conf.settings ') set up a large global dictionary os, place exposed to the path of the user's settings file
will be loaded global configuration, and then load the user profile, if the user is configured, the global configuration will top off.
The item will not set the default configuration, to implement user-defined settings internal functional configuration
will first default configuration for loop using dir (global configuration module) to get a list of the names of all the strings in the form of the module, then name judge,
because if you want to customize the configuration, variable names must be capitalized, so by the line filter, will meet the requirements of configuration settings stored using setattr objects in the form of key-value pairs
and then from the global large dictionary, from the os Dictionary of obtaining global settings file path exposed to the user using import_module path as a string import module
and the module for loop, all arranged to obtain the user's profile is exposed to, in order to keep the form of key-value pairs settings object into

 

Django user settings to achieve the configuration on the use of user-defined configuration settings, the user is not configured to use django global settings of
three things happens import module:
1. Create a namespace operation of the module
2. Create the imported module module name space, all the code and executes the module, the name of the generated completely in this name space
3. the execution then generates a file name, point name of the module is introduced into the space



admin five configuration parameters
when the user does not self- when defining the method. admin will use the default ModelAdmin. When the user-defined methods, uses the user-defined method
list_display = [ 'field name'] control (Control Display field) display fields in admin page

list_display_links = [ 'field name'] can be screened in the background screening field. (Jump control field)

search_fields = [ 'field name'] query, a query to the field name through the line, when the query is "OR", (or query)

list_filter = [ 'foreign key field name'] Right side filter foreign key fields added to the inside, will be screened for the corresponding data (if there is more than fields, can query one filter,
can be added to many to many fields, when the filter is in the "aND" such as the first screening relationship names, followed by screening of the book it is written, and operation)



# batch modify the value. Change the value specified in queryset_update (field name = 'new value') can be selected fields
DEF path_init (Self, Request, QuerySet):
queryset.update (. Price = 666)
path_init.short_description = 'price bulk edit'
Actions = [path_init]


concept class configuration


admin source code analysis
django will start admin.py sequentially performed for each file in the app application
module is executed at the time of import __all__ method. When the file is imported. All names can be used

 

# Import module below, with the function definition. Django every decision will be activated when the function is executed, it will go looking for each application under admin.py file
from django.utils.module_loading Import autodiscover_modules
DEF autodiscover ():
autodiscover_modules ( 'ADMIN', register_to = Site) # If the 'admin' replaced by another, the other will be executed.



Examples of ways to achieve a single


1. Based on a classmethod
2. Based decorator
3. metaclass based on the __call__
4. Based __new__ is
5. The module based



Guess you like

Origin www.cnblogs.com/liguodeboke/p/11280323.html