django第九天总结

###day22
- 1.资产变更记录
- 2.后台管理
- 3.传统方式对业务线 增删改查
- 4.提供一个增删改查的组件 

###内容回顾:
- 1.cmdb 
    - 架构:
        - 资产采集的部分  client 
        - api django  djangorestframework 
        - 后台管理
        
- 2.技术点 
    - 1.开发封闭原则
        - 源码  封闭
        - 配置  开放
        
        - 兼容多种模式并且可扩展
            - agent    subprocess 
            - ssh         paramiko
            - salt/ansible
        - 采集硬件信息的可插拔式设计
            - importlib   导入模块
            - 反射 
                
    - 2.类的约束
        - 继承  +  抛出异常
        - 抽象类 + 抽象方法
        
    - 3.错误详情
        - traceback.format_exc()
        
    - 4.唯一标识 
        - 主机名
        
    - 5.requests 发请求
        
    - 6.线程池
        - from concurrent.futures import ThreadPoolExecutor
        
    - 7.api校验
        - key|时间戳    MD5
        - key|时间戳  time 
        - key只能用一次
        - 时间上的判断
        
    - 8.数据加密
        - rsa   
        
    - 9.api     
        - CBV djangorestframework
        - from rest_framework.views import APIView
        - from rest_framework.response import Response
    
###准备知识
```python
    class  Base():
        def f1():
            print('Base f1')
            self.f2()
            
        def f2():
            print('Base f2')

    
    class  Foo(Base):
        
        def f2():
            print('Foo f2')

    c1 = Foo()
    c1.f1()
```
    
```python
    Class Base()
        def f1():
            print('Base f1')
        

    Class Foo(Base)
        def f1():
            super().f1()
            print('Foo f1')
    
    
    c1 = Foo()
    c1.f1()
```    

```python    
    class Base():
        def f1():
            print('Base f1')
        

    class Foo(Base):
        def f1():
            super().f1()
            print('Foo f1')
    
    class Bar(Base):
        def f1():
            super().f1()
            print('Bar f1')
            
    class D(Foo,Bar):
        pass
        
    d = D()
    d.f1()
```
    
- super  按照继承顺序找当前类之后的类里的方法
    
###展示:
- 1.普通字段
    - obj.filed
    
- 2.choice  ((1,''),)
    - obj.filed   _>   1
    - obj.get_字段名_display()   男
    
- 3.外键 
    - obj.外键字段   _>  关联的对象   __str__
    - obj.外键字段.name
    
- 4.自定义方法
    - 展示多对多 
    - 加css样式
    
    ```python
    from django.utils.safestring import mark_safe
    def show_s(self):
        return mark_safe('<span style="color:red">xxxxxx</span>')
    ```
    

###使用stark组件的步骤:
- 1.拷贝stark组件到django项目中并注册
    - 注册:
     - 'stark.apps.StarkConfig'
    
- 2.在已经注册的app下创建stark.py文件
- 3.使用stark组件的路由
```python
    from django.conf.urls import url,include

    from stark.service.stark import site
    urlpatterns = [
        url(r'^stark/',include(site.urls))
    ]
```
- 4.在stark.py文件写配置并注册
```python
    from stark.service.stark import site,StarkConfig
    from repository import models


    class BusinessUnitConfig(StarkConfig):
        # 展示字段
        list_display = ['id','name']
        # 模糊搜索
        search_list  = ['name']
        # 排序
        order_by = ['name','-id']


    site.register(models.BusinessUnit,BusinessUnitConfig)
```        
原文地址:https://www.cnblogs.com/lilyxiaoyy/p/11567521.html