Analog django Admin

--- --- restore content begins

1 Analog admi start automatically execute admin-related applications, where we will own file called myadmin, create myadmin.py file in our application app01

Write fixing method in myadmin application we created in the app, using atuodiscover_moudels automatically retrieves the file name and execute the file in the startup first time

from django.apps import AppConfig
from django.utils.module_loading import autodiscover_modules

class MyadminConfig(AppConfig):
    name = 'myadmin'
    def ready(self):
        return autodiscover_modules('myadmin')

Admin registered 2 simulation method, write out the analog source, create a service file in myadmin file, a name of my own blind plays, create myadmin in this document

Register source data admin django write simulation model table, the registered classes correspond to tables and models configuration class

配置类类名
class ModelMyadmin(object):





#注册模型表源码
class MyadminSite(object):
    def __init__(self, name='admin'):
        self._registry = {}  # model_class class -> admin_class instance

    def register(self, model, admin_class=None, **options):
        if not admin_class:
            admin_class = ModelMyadmin
            # Instantiate the admin class to save in the registry
        self._registry[model] = admin_class(model)

Key : In the configuration class self is our operating model table,

Setting a Route

 First, to find out the nature of packet routing, he is in front of the name url wrapped routing route, may be followed by a comma behind the view function is the function name, or the name of the two routing function plus the function name of the view, in plus two None, should be noted that the back of a route no matter what must be in the form of tuples to wrap up.

Here route generating analog admin routes generated in the generating source category and register configuration of the model table.

Model generates a routing table to view the source code in the registration:

   DEF get_urls (Self): 

        urls = []
         for model_class, config_obj in self._registry.items ():       
       config_obj got the model correspond to the table configuration class
# us by registering in a big dictionary in the source code can be --meta .app_label model table where the application name removed, and # _meta.model_name extraction model table app_label = model_class._meta.app_label MODEL_NAME = model_class._meta.model_name # formed by generating a routing urls after splicing, urls.append ( URL (R & lt ' ^% S /% S / ' % (app_label, MODEL_NAME), config_obj.urls) ) return urls @property def urls(self): return self.get_urls(),None,None

 Two routing tables to relate to the operation of the data model, where registration can not be generated in the source code, because the template is formed after the source into the use of a singleton pattern, if generated two source register routing tables so that all operations are generated It is the same, and

However, the specific operation of the table and therefore can not be in the same model classes correspond to tables arranged to generate two routes:

 @property
    def urls(self):
        urls = [
            url(r'^$',self.list_view,name='%s_%s_%s'%(self.app_label,self.model_name,'list')),
            url(r'^add/',self.add_view,name='%s_%s_%s'%(self.app_label,self.model_name,'add')),
            url(r'^update/(\d+)/',self.update_view,name='%s_%s_%s'%(self.app_label,self.model_name,'update')),
            url(r'^delete/(\d+)/',self.delete_view,name='%s_%s_%s'%(self.app_label,self.model_name,'delete')),
        ]
        return urls,None,None

After generating the routing table model is designed to show the operating table, and the table data change search deletions;

Wherein at the table to show the user in the end we do not know what data to show, so we set up a default configuration class display, only a display model of the table object through the display object name __str__ method. Provided to the user can customize the method of exposed user registry, users write on their own if you do not write our own is to use the default user with the user's
default configuration list_play in class

class ModelMyadmin(object):
    list_play = ['__str__',]

In the configuration class is determined if the user does not do list_play provided, then the default

 def get_new_list_play(self):
        tmp = []
        tmp.append(ModelMyadmin.check_col)
        tmp.extend(self.list_play)
        if not self.list_play_links:
            tmp.append(ModelMyadmin.edit_col)
        tmp.append(ModelMyadmin.delete_col)
        return tmp

Display data content reception using table tags for rendering processing on the data processing is necessary to form the background into the header and contents of the form to show to show, with the Chinese want to show the header, you can now model table settings such as: verbose_name = 'books were,

In the method using the configuration class ._meta to point out the getfield () method to get verbose_name

Header shows:

  def get_head(self):
        head_list = []
        for field_or_func in self.config_obj.get_new_list_play():
            if isinstance(field_or_func, str):
                if field_or_func == '__str__':
                    head_list.append((self.config_obj.model._meta.model_name).upper())
                else:
                    val = self.config_obj.model._meta.get_field(field_or_func).verbose_name
                    head_list.append(val)
            else:
                val = field_or_func(self.config_obj, is_head=True)
                head_list.append(val)
        return head_list

Form content display:

    def get_body(self):
        # 表单展示
        body_list = []
        for obj in self.page_queryset:
            print(obj.pk)
            tmp = []
            for field_or_func in self.config_obj.get_new_list_play():
                if isinstance(field_or_func, str):
                    val = getattr(obj, field_or_func)
                    if field_or_func in self.config_obj.list_play_links:
                        _url = self.config_obj.get_reverse('update', obj)
                        val = mark_safe('<a href="%s">%s</a>' % (_url, val))
                else:
                    val = field_or_func(self.config_obj, obj=obj)
                tmp.append(val)
            body_list.append(tmp)
        return body_list

 

 

--- end --- restore content

Guess you like

Origin www.cnblogs.com/1624413646hxy/p/11299109.html