首页 > 其他 > 详细

Django如何使文件在django自动执行

时间:2018-03-24 13:20:47      阅读:272      评论:0      收藏:0      [点我收藏+]

1. Django admin源码中

admin.py

from django.contrib import admin

# Register your models here.

在admin源码中

# ACTION_CHECKBOX_NAME is unused, but should stay since its import from here
# has been referenced in documentation.
from django.contrib.admin.decorators import register
from django.contrib.admin.filters import (
    AllValuesFieldListFilter, BooleanFieldListFilter, ChoicesFieldListFilter,
    DateFieldListFilter, FieldListFilter, ListFilter, RelatedFieldListFilter,
    RelatedOnlyFieldListFilter, SimpleListFilter,
)
from django.contrib.admin.helpers import ACTION_CHECKBOX_NAME
from django.contrib.admin.options import (
    HORIZONTAL, VERTICAL, ModelAdmin, StackedInline, TabularInline,
)
from django.contrib.admin.sites import AdminSite, site
from django.utils.module_loading import autodiscover_modules

__all__ = [
    "register", "ACTION_CHECKBOX_NAME", "ModelAdmin", "HORIZONTAL", "VERTICAL",
    "StackedInline", "TabularInline", "AdminSite", "site", "ListFilter",
    "SimpleListFilter", "FieldListFilter", "BooleanFieldListFilter",
    "RelatedFieldListFilter", "ChoicesFieldListFilter", "DateFieldListFilter",
    "AllValuesFieldListFilter", "RelatedOnlyFieldListFilter", "autodiscover",
]


# 通过autodiscover来实现自动发现文件
def autodiscover():
    autodiscover_modules(admin, register_to=site)


default_app_config = django.contrib.admin.apps.AdminConfig

2. 自定义一个项目

python  manage.py startapp vbc

3. 将vbc项目加入到自动发现

修改apps.py文件

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

class StarkConfig(AppConfig):
    name = stark

    def ready(self):
        autodiscover_modules(vbc)

 

Django如何使文件在django自动执行

原文:https://www.cnblogs.com/supery007/p/8638393.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!