Skip to content

Handling Missing Dynamic Models in Django's app_configs #328

@rajatb2814

Description

@rajatb2814

Currently, models present in packages or modules are not initialized in app_configs of apps within the django.apps module.
This leads to issue where the models are not returned with apps.get_models(). This have implications on some default functions given by ContentType model like model_class()

Proposed Solution:

To address this, I propose an enhancement to dynamically update app_configs with models discovered at runtime. Specifically, we can introduce a mechanism to update app_configs with dynamic models when running the server or executing commands.

Implementation Details:

Retrieve Dynamic Models: Obtain a list of all dynamic models that need to be registered.
Update app_configs: Modify the app_configs dictionary to include these dynamic models. This can be achieved by updating the models attribute of the relevant AppConfig instances.

Example Code Snippet:

from django.apps import apps
app_config = apps.app_configs.get('dynamic_models')
if app_config:
app_config.models.update({
'<model_name>': <model_class>
})

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions