+ Orm depth assignment operation simple configuration + Django-admin

Knowledge
depth copy
shallow depth value of the above mentioned id
ORM operating
ManyToManyField virtual field
tells Django orm automatically help you create a third table
query time can make use of the field across the table
foreign key attribute assignment outreach objects
Model.object under 13 will be operating
returns QuerySet object methods
All ()
filter ()
the exclude ()
ORDER_BY ()
Reverse ()
DISTINCT ()
special QuerySet
values () returns an iterative dictionary sequence
values_list () returns the ancestral sequence an iterative
returns particular subject the
GET ()
First ()
Last ()
returns a boolean methods are:
EXISTS ()
returns the method has the
count ()
a foreign key
the Add
Remove
SET
above three may transmit one or more parameters (data types may iteration) and which can be a digital object may be
clear
not clear pass parameters directly
above process will flush cache use, it is not necessary to use save ().
Cross-table query
Cross-table query based on the object (all sub-queries), '' connection
object-based reverse lookup in addition to direct one to one point lowercase table names can get the associated object
-to-many-many reverse a must watch _set lowercase name
based on cross-table queries double-underlined (are linked table query)
key: forward reverse field table name in lowercase
n - reverse lookup
foreign key forward, according to the foreign key field
is reversed foreign keys, press lowercase table names

8 | 0Django terminal print SQL statements

if __name__ == '__main__':
    作为脚本执行时内容
else:
    作为模块时内容

查询集
    Django中的QuerySet本质上是一个懒加载的对象,作用是进行表级操作,返回结果集(一张视图:类型<class 'django.db.models.query.QuerySet'>值<QuerySet [<Person: Person object (1)>, <Person: Person object (2)>]>或一条记录:类型<class 'TestModels.models.Person'>值Person object (1)),当结果集是视图时可链式调用。<QuerySet [<Person: Person object (1)>, <Person: Person object (2)>]>
URL路由反向解析---后台反向解析
    django.urls.reverse(name,args)

Only by understanding and remember to be your own. Is the future of learning content to do subtraction. Closer to nature, simple model, the more conducive to understand and remember. Learning, the use, the better the effect of reducing the burden.
10m to 100m people meteorites regional 1km to 10km continent of the planet meteorite

Source settings
algorithm
summarized: manage.py provided 'DJANGO_SETTINGS_MODULE' default value 'untitled.settings' module string, and then set by the above method to read global 'global_settings' is again written to the user dictionary setting dictionary (advantageous : using the re-assignment key set by the user to achieve a high priority, and the user setting parameters scalability)
'the DJANGO_SETTINGS_MODULE' = 'untitled.settings'
'the DJANGO_SETTINGS_MODULE' + 'global_settings' -> 'ENVIRONMENT_VARIABLE'
third-party storage (e.g. os.environ) exposed to the user setting file name import module, configured to store a class setting key pair, introducing two setting module (user module setting file name is read from the first to third module reuse importlib introduced) sequentially using getattr (), setattr () will be disposed within the namespace two key modules writing such
knowledge
importlib module
using importlib can import module name string 'module represented
os with SYS
os program module is responsible for the operation interactive system, provides the interface to access the underlying operating system; sys module is responsible for the program and the python interpreter Interaction, provides a range of functions and variables to control when running python environment.
Test project startup file
/xxx.py
Import os
Import SYS
base_dir = os.path.
sys.path.append (base_dir)
· · ·
from importlib module
getattr (namespace (module name)), setattr (object, name , value)

admin user configuration file
configuration type, inherit admin.ModelAdmin class
five configuration parameters
list_display control display fields careful not to put many to many fields
list_display_links link control field shows careful not to put many to many fields
search_fields multi-field retrieval careful not to put many to many fields
list_filter multi screening list for a discharge foreign key field
class BookConfig (admin.ModelAdmin):
list_display = [ 'title', '. price', 'the publish_date', 'publish']
list_display_links = [ 'title', '. price']
search_fields = [ ' title ','. price ']
list_filter = [' publish ',' the authors']
DEF patch_init (Self, Request, QuerySet): # fixed parameters
queryset.update (price = 666) # 2.queryset logical operation method selection set
patch_init. short_description = 'price bulk edit' # 3. The method referred
actions = [patch_init] # 1.actions batch operation frame handler column method
admin.site.register (models.Book,BookConfig)

admin operating logic
1.admin source start
django admin.py be sequentially performed for each file in the application when it starts
manage.py-> settings.py ~~~ 'django.contrib.admin' - > autodiscover_modules ( 'admin' , register_to = site) -.> AdminSiteClass = import_string (apps.get_app_config ( 'admin') default_site)
singleton
2. Source Register
admin.py registration statement
admin.site.register (models.Publish) # is registered only in the model table and the parameter table model object instantiated generated

As a key-value pair into a _registry field site object

3.URL
    admin会给每一个注册了的表生成增删改查四条url

Routing distributed nature of
the routing: url ( '', func, None, None) can be distributed to the iterative nature of the routing function is replaced with a view routing columns, multistage replace standard routing function distribution group is a view

    个性化后台管理应用设计与实现
stark(自定义后台管理应用应用名)
    启动 通过stark读取其他应用中的stark.py
        令django一启动就要执行每一个应用下的stark.py文件#该文件如果被用户在应用下创建,替代应用下的admin.py文件
        算法:
            配置文件中注册:
                INSTALLED_APPS = [
                    'stark.apps.StarkConfig',
                ]           
            app配置模块apps.py 添加:
                def ready(self):#函数重写
                    from django.utils.module_loading import autodiscover_modules
                    return autodiscover_modules('stark')
    注册 在其他应用admin.py和urls.py中替代django.contrib.admin.site使用
        应用下创建stark.py
            快复制Django默认admin配置 django.contrib.admin.sites和django.contrib.admin.ModelAdmin中的内容,根据需要重写方法
            算法:
                site=管理站点类(admin中是AdminSite()),
                管理站点类中需要用到模型管理类(admin中是django.contrib.admin.ModelAdmin),这两个类中的诸般方法根据用户需要重写
            1.二级路由分发
                为了实现二级路由生成,一级路由写在管理站点类,二级路有写在模型管理类,因为:
                    class ModelStark(object):
                        list_display = ['__str__',]
                        def __init__(self,model):
                            self.model = model

[Distributed learning python reptile - from foundation to combat]

Guess you like

Origin blog.51cto.com/12486145/2452593