Django INSTALLED_APPS的命名约定如何工作? [英] How does it work, the naming convention for Django INSTALLED_APPS?

查看:92
本文介绍了Django INSTALLED_APPS的命名约定如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该站点上的教程创建了一个名为polls的应用程序。它使用的是django 1.9,因此在INSTALLED_APPS中为:

The tutorial on the site creates an app named polls. It's using django 1.9, so in the INSTALLED_APPS it is:

polls.apps.PollsConfig

我正在看一个教程,他给应用程序命名,并在INSTALLED_APPS中拥有

I'm watching a tutorial he names the app newsletter and in INSTALLED_APPS he has

newsletter

他使用的是1.8。我正在使用1.9。我看过其他教程,他们也像他一样在语法中添加了一个不带点的名称。我知道事情可能有所不同,这是可以理解的。要明确的是,我是否将我的应用程序狗命名为。在已安装的应用程序中,它将被命名为

he's using 1.8, though. I am using 1.9. I've watched other tutorials and they also just add a name without dots in the syntax as he does. I realize things may be different, that's understood. To be clear if I named my app dogs,. in the installed apps it would be named like this

dogs.apps.DogsConfig

或者如果是树,则是

tree.apps.TreeConfig

命名约定是这样吗?我还认为在新版本中事情会变得更短,更方便。因此,只需添加

Is that how the naming convention goes? also I would assume things would get shorter in newer versions and more convenient. so to go from just adding

newsletter,

必须键入

polls.apps.PollsConfig

对我来说似乎很奇怪。但是我是新来的,所以我可能会错过一些东西。欢迎任何建议

seems weird to me. But I'm new so I maybe missing something. Any and all advice is welcome

推荐答案

应用程序配置 功能,是Django 1.7的新功能。

That is the Application Configuration feature, new to Django 1.7.

基本上,现在您可以列出在 INSTALLED_APPS 中,包含应用程序的模块或从 django.apps.AppConfig 派生并定义行为的类

Basically, now you can list in INSTALLED_APPS either the module that contains the application or a class that derives from django.apps.AppConfig and defines the behavior of the application.

此功能具有以下优点:


  • 应用可以

  • 您可以在同一模块中拥有多个应用。

应用程序模块可以定义特殊的模块变量 default_app_config 来指定其 AppConfig 的名称,以便它们可以使用新功能,而不必在 INSTALLED_APPS 中指定该类的全名。但这是向后兼容的功能,建议新应用程序编写完整的 AppConfig 名称。

Application modules can define the special module variable default_app_config to specify the name of their AppConfig, so that they can use the new features without having to specify the full name of that class in INSTALLED_APPS. But this is a backwards compatibility feature and new applications are recommended to write the full AppConfig name.

无论如何,大多数 django / contrib 应用程序使用 default_app_config 来与旧配置兼容。例如,参见文件 django / contrib / messages / __ init __。py 只是:

Anyway, most django/contrib apps use that default_app_config, for compatibility with old configurations. See for example the file django/contrib/messages/__init__.py is just:

from django.contrib.messages.api import *
from django.contrib.messages.constants import *

default_app_config = 'django.contrib.messages.apps.MessagesConfig'

因此,根据每个OP请求将其加起来:

So, adding it up, per OP request:


  • 如果在 INSTALLED_APPS 中添加类型名称 foo.apps.FooConfig ,则该类将用于设置1.7样式的 foo 应用程序(推荐)。

  • 如果添加 INSTALLED_APPS 原名 foo ,然后:

  • If you add in INSTALLED_APPS the typename foo.apps.FooConfig, then that class will be used to setup the foo app, 1.7 style (recommended).
  • If you add in INSTALLED_APPS the plain name foo, then:


  • 如果存在变量 foo.default_app_config ,则该类将用于设置 foo 应用程序1.7风格。从大多数(全部?)标准Django应用程序都有此变量,因此从Django-1.6升级到Django-1.7时,无需更改 INSTALLED_APPS

  • 如果没有这样的变量,则将使用1.6样式的应用程序,并使用高级配置选项的默认值。

  • if there is a variable foo.default_app_config this class will be used to setup the foo app, 1.7 style. Most (all?) the standard Django apps have this variable, so that you don't need to change your INSTALLED_APPS when you upgrade from Django-1.6 to Django-1.7.
  • if there is not such a variable, then the 1.6 style application will be used, with default values for the advanced configuration options.

这篇关于Django INSTALLED_APPS的命名约定如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆