django.app-AppConfig

AppConfig既然是app的设置类,那么它有哪些属性?

通过__init__方法初始化的属性:

name: app的路径名     比如:django.contrib.admin
module: app的模块


通过继承AppConfig,定义类属性:

label:唯一的名字,默认是name的最后部分。比如:admin
verbose_name: 别名。默认为label.title( )
path:app的文件路径名。


django自动生成的属性:

models_module   对应app下面model模块
models    字典 {model_name: model_class}



然后再看看怎么在django中,设置自己的Appconfig:

Appconfig主要由它的类方法create(cls, entry)来实例化。

entry即为setting中的INSTALLED_APPS的元素。

@classmethod
    def create(cls, entry):
        """
        Factory that creates an app config from an entry in INSTALLED_APPS.
        """
        try:
            # If import_module succeeds, entry is a path to an app module,
            # which may specify an app config class with default_app_config.
            # Otherwise, entry is a path to an app config class or an error.
            module = import_module(entry)

        except ImportError:
            # Track that importing as an app module failed. If importing as an
            # app config class fails too, we'll trigger the ImportError again.
            module = None

            mod_path, _, cls_name = entry.rpartition('.')

            # Raise the original exception when entry cannot be a path to an
            # app config class.
            if not mod_path:
                raise

        else:
            try:
                # If this works, the app module specifies an app config class.
                entry = module.default_app_config
            except AttributeError:
                # Otherwise, it simply uses the default app config class.
                return cls(entry, module)
            else:
                mod_path, _, cls_name = entry.rpartition('.')

        # If we're reaching this point, we must attempt to load the app config
        # class located at <mod_path>.<cls_name>

        # Avoid django.utils.module_loading.import_by_path because it
        # masks errors -- it reraises ImportError as ImproperlyConfigured.
        mod = import_module(mod_path)
        try:
            cls = getattr(mod, cls_name)
        except AttributeError:
            if module is None:
                # If importing as an app module failed, that error probably
                # contains the most informative traceback. Trigger it again.
                import_module(entry)
            else:
                raise

        # Check for obvious errors. (This check prevents duck typing, but
        # it could be removed if it became a problem in practice.)
        if not issubclass(cls, AppConfig):
            raise ImproperlyConfigured(
                "'%s' isn't a subclass of AppConfig." % entry)

        # Obtain app name here rather than in AppClass.__init__ to keep
        # all error checking for entries in INSTALLED_APPS in one place.
        try:
            app_name = cls.name
        except AttributeError:
            raise ImproperlyConfigured(
                "'%s' must supply a name attribute." % entry)

        # Ensure app_name points to a valid module.
        app_module = import_module(app_name)

        # Entry is a path to an app config class.
        return cls(app_name, app_module)

整个导入流程如图所示:

000900_ThLJ_569730.jpg


可以看见有两个方法,一个是指明AppConfig的位置,模块名.类名。

# settings.py

INSTALLED_APPS = (
    ...,
    'rock_n_roll.apps.RockNRollConfig',
    ...
)



二是在模块__init__.py文件中,声明default_app_config属性。default_app_config为字符串,指定AppConfig的位置。

引用官网的例子:

rock_n_roll是一个app。这是下面的app.py文件

# rock_n_roll/apps.py
from django.apps import AppConfigclass 

RockNRollConfig(AppConfig):
    name = 'rock_n_roll'
    verbose_name = "Rock ’n’ roll"

# rock_n_roll/__init__.py

default_app_config = 'rock_n_roll.apps.RockNRollConfig'


不过一般使用第二种比较多,因为这样保证了INSTALLED_APPS设置的一致性。

转载于:https://my.oschina.net/u/569730/blog/358815

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WARNINGS: ?: (staticfiles.W004) The directory 'static' in the STATICFILES_DIRS setting does not exist. buy.Cart: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'. HINT: Configure the DEFAULT_AUTO_FIELD setting or the BuyConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django.d b.models.BigAutoField'. buy.Orders: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'. HINT: Configure the DEFAULT_AUTO_FIELD setting or the BuyConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django.d b.models.BigAutoField'. sale.Brand: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'. HINT: Configure the DEFAULT_AUTO_FIELD setting or the SaleConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django. db.models.BigAutoField'. sale.Carinfo: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'. HINT: Configure the DEFAULT_AUTO_FIELD setting or the SaleConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django. db.models.BigAutoField'. userinfo.UserInfo: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'. HINT: Configure the DEFAULT_AUTO_FIELD setting or the UserinfoConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'dja ngo.db.models.BigAutoField'. No changes detected
06-02

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值