Skip to content

Django

前言: 暂时没想到有什么说的,后面补充吧。。。

一、Django简介

二、Middleware 中间件

在请求阶段,调用视图之前,Django会按照MIDDLEWARE_CLASSES中定义的顺序自顶向下应用中间件。会用到两个钩子: 在响应阶段,调用视图之后,中间件会按照相反的顺序应用,自底向上。会用到三个钩子:

  • process_request()
  • process_view()
  • process_exception()
  • process_template_response()
  • process_response()

三、请求流程

四、Model

2.1 ForeignKey 1) on_delete - CASCADE:这就是默认的选项,级联删除,你无需显性指定它。 - PROTECT: 保护模式,如果采用该选项,删除的时候,会抛出 ProtectedError 错误。 - SET_NULL: 置空模式,删除的时候,外键字段被设置为空,前提就是blank=True, null=True,定义该字段的时候,允许为空。 - SET_DEFAULT: 置默认值,删除的时候,外键字段设置为默认值,所以定义外键的时候注意加上一个默认值。

五、ORM

orm sql
两个'_' 关联表属性
filter =
exclude !=
querySet.distinct() 去重复
__(i)exact 精确等于 like 'aaa',精确等于 忽略大小写 ilike 'aaa'
__(i)contains 包含(忽略大小写) (i)like '%aaa%',但 sqlite ,contains = icontains
__(i)startswith 以...开头(忽略大小写)
__(i)endswith 以...结尾(忽略大小写)
__gt(e) 大于(等于)
__lt(e) 小于(等于)
__in 存在于一个list范围内
__range 在...范围内
__year(month/day) 日期字段的年份(月份/日)
__isnull True/False

六、Admin

  1. App 下,编辑 admin.py ,创建相应的管理功能

    from django.contrib import admin
    
    @admin.register(XXX)
    class XXXAdmin(admin.ModelAdmin):
        xxx
    

  2. admin 属性、方法,可参考 ModelAdmin

    class ModelAdmin(BaseModelAdmin):
        """Encapsulate all admin options and functionality for a given model."""
    
        list_display = ('__str__',)
        list_display_links = ()
        list_filter = ()
        list_select_related = False
        list_per_page = 100
        list_max_show_all = 200
        list_editable = ()
        search_fields = ()
        date_hierarchy = None
        save_as = False
        save_as_continue = True
        save_on_top = False
        paginator = Paginator
        preserve_filters = True
        inlines = []
    
        # Custom templates (designed to be over-ridden in subclasses)
        add_form_template = None
        change_form_template = None
        change_list_template = None
        delete_confirmation_template = None
        delete_selected_confirmation_template = None
        object_history_template = None
        popup_response_template = None
    
        # Actions
        actions = []
        action_form = helpers.ActionForm
        actions_on_top = True
        actions_on_bottom = False
        actions_selection_counter = True
        checks_class = ModelAdminChecks
    

七、Form

八、第三方插件

第三方库 功能介绍 推荐指数
django-extensions 拓展库,包含常用的基础 Model,ER 图导出 ★★★★★
django-model-utils 拓展库,常用基础 Model、Field 等 ★★★★★
django-mdeditor Markdown 编辑器 ★★★★★
django-environ ★★★★★
django-storages[boto3] 媒体、静态资源存储 ★★★★★
django-celery-beat Celery 动态定时任务 ★★★★★
django-click
django-fsm
django-contact-form
django-allauth 全登录认证 ★★★★★
django-rest-auth
django-rest-swagger API文档化 ★★★★☆
django-notifications 消息通
django-activity-stream 活动流
django-rest-framework REST-ful API
django-debug-toolbar 调试工具
django-social-auth 社交授权认证
channels 即时通信
django-celery 定时服务
django-registration 注册登记?
django-analytical 搜索分析
django-robots 爬虫管控
django-rest-framework-jwt jwt
django-socketio socketio
django-user-accounts 账号功能,注册激活邮件等
django-dynamic-scraper django与scraper的集成
django-haystack 提供模块化搜索(elasticsearch)
django-configurations 组织配置?
django-hosts 组织host?
django-discover-jenkins DI
cookiecutter-django 脚手架项

https://www.cnblogs.com/wumingxiaoyao/p/6928297.html