1

I read about this,And i cunfused

The application server takes the INSTALLED_APPS and loads or generates an AppConfig object for each app. This collection of AppConfig objects are then stored in an instance of theApps class.

Please someone explain with simple example. Thanks

aaaa
  • 73
  • 1
  • 6
  • 2
    [official documentation django](https://docs.djangoproject.com/en/4.2/ref/applications/) – sahasrara62 Aug 10 '23 at 03:49
  • Well ... an `apps.py` module is where you put the `AppConfig` classes so that Django can find them. And the `AppConfig` classes (can) define an app; e.g. if you need to define it in a way that differs from the (default) on-the-fly generated app config. – Stephen C Aug 10 '23 at 03:50
  • You can read my answer here to know difference between apps and project with an example if you are confused about that as well: https://stackoverflow.com/questions/19350785/what-s-the-difference-between-a-project-and-an-app-in-django-world/54490497#54490497 – Ahtisham Aug 10 '23 at 04:09

3 Answers3

2

I hope this doesn't confuse you more. But I'll do my best.

Let's say you ran python manage.py startapp hello django creates directories/files for you by default including the apps.py.

But in order to use the hello app you have to add it in INSTALLED_APPS project settings.

When you ran python3 manage.py runserver it does this (in summary):

  • first loads your project's settings.py
  • Then goes to each of the INSTALLED_APPS
  • creates an 'app class' for each of the installed apps. It will use what's in apps.py by default if it exists. Otherwise, it will just load the base AppConfig.

^ That answers: The application server takes the INSTALLED_APPS and loads or generates an AppConfig object for each app

For this context: This collection of AppConfig objects are then stored in an instance of theApps class.

In my example above, it created a class helloConfig(AppConfig). The class helloConfig inherited from AppConfig.

If I were to create an instance it will looks like this.

myinstance = helloConfig()
myinstance #-> `collection of AppConfig objects are then stored` in the `myinstance`

Imagine Django doing that for each of the Installed app with the respect to the apps' name.

Additional side note, It's not required for the every apps to have an apps.py but AppConfig has other attributes that may be useful. If you want to utilize those attributes, django creates a class wrapper (class helloConfig) that inherits from the AppConfig.

This way you can use def ready() which is one of the attributes in AppConfig (without editing AppConfig's ready method directly)

class HelloConfig(AppConfig):
    ...

    def ready():
       # Run this when the Hello app is ready
0

This file is created to help the user include any application configuration for the app. Using this, you can configure some of the attributes of the application.

dev0717
  • 177
  • 4
0

It's a configuration file. You can define metadata and settings. You can also override the ready() method if needed.

Debbie
  • 11
  • 2