Django中settings.py的Installed Apps里‘myapp’与‘myapp.apps.myappConfig’的区别
Great question! Let’s break down the key differences between these two ways of registering your app in Django’s INSTALLED_APPS, and how they tie into your models.
Key Differences Between
'myapp' and 'myapp.apps.MyappConfig' 1. App Loading Behavior
- When you add
'myapp'toINSTALLED_APPS, Django tries to auto-detect an app configuration class. It looks for anapps.pyfile in your app directory; if there’s a subclass ofAppConfig(like the auto-generatedMyappConfig), it uses that. If no custom config exists, it falls back to a generic defaultAppConfiginstance. - Adding
'myapp.apps.MyappConfig'is an explicit choice: you’re telling Django exactly which config class to use to initialize your app, skipping the auto-detection step.
2. Customization Capabilities
The biggest difference comes down to control. Explicit config classes let you customize your app’s behavior in apps.py:
- Set a human-readable
verbose_name(this shows up in the Django admin interface for your app) - Define a
default_auto_fieldto set a consistent primary key type for all models in the app that don’t specify their own - Run one-time initialization code in the
ready()method (like registering model signals, loading custom utilities, or connecting to external services)
With the shorthand 'myapp', none of these customizations will work unless you add default_app_config = 'myapp.apps.MyappConfig' to your app’s __init__.py to make your custom config the default.
3. Direct Impact on Models
Yes, this difference absolutely relates to your models! Here’s how:
- Model Discovery: If your app uses multiple model files (e.g., a
models/folder with separate files for different models), you can use themodels_moduleattribute in your custom config to point Django to the right module, or import those models explicitly inready()to ensure they’re detected. The shorthand'myapp'only scans the rootmodels.pyby default. - Default Primary Key Consistency: Setting
default_auto_fieldin yourMyappConfiglets you enforce a consistent primary key type (likeBigAutoField) for all models in the app. With'myapp', you’re limited to the globalDEFAULT_AUTO_FIELDsetting fromsettings.pyunless you’ve set the default app config. - Signal Registration: If you have model-related signals (e.g.,
post_savetriggers to update related data), putting their import inready()ensures they’re registered when the app loads. The shorthand'myapp'won’t run thisready()code unless you’ve configured the default app config.
Example Custom App Config
Here’s a typical apps.py that demonstrates these features:
from django.apps import AppConfig class MyappConfig(AppConfig): default_auto_field = 'django.db.models.BigAutoField' name = 'myapp' verbose_name = 'My Custom Application' def ready(self): # Ensure model signals are registered when the app loads import myapp.signals
Quick Recap
- Use
'myapp'for simple apps where you don’t need any custom app behavior—it’s a handy shortcut. - Use
'myapp.apps.MyappConfig'when you need control over your app’s initialization, custom admin labels, consistent model primary keys, or model-related signal handling.
内容的提问来源于stack exchange,提问作者notVansh
相关产品推荐
相关产品推荐

