[Django] #36604: Add utility for lazy model resolution by string label (e.g. "app_label.ModelName")

4 views
Skip to first unread message

Django

unread,
Sep 10, 2025, 9:31:54 PM (10 days ago) Sep 10
to django-...@googlegroups.com
#36604: Add utility for lazy model resolution by string label (e.g.
"app_label.ModelName")
----------------------------------------+------------------------------
Reporter: Mbulelo_Peyi | Owner: Mbulelo_Peyi
Type: New feature | Status: assigned
Component: Utilities | Version: 5.2
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 1
UI/UX: 0 |
----------------------------------------+------------------------------
This feature proposes the addition of two utility functions to simplify
lazy model resolution in Django:

- get_model_by_label(model_label: str)
Resolves and returns a model class from a string label like
'app_label.ModelName'.

- lazy_model(model_label: str)
Returns a `SimpleLazyObject` that lazily resolves a model using the
above utility.

These functions are useful when:
- Avoiding circular imports (e.g., in apps.py, signals.py, or settings.py)
- Dynamically referencing models by name
- Loading models safely before the app registry is fully ready

This pattern is currently re-implemented in many Django projects and
third-party packages. Including it in Django would standardize a safe,
performant, and readable approach to lazy model loading.

The get_model_by_label() function includes:
- Input validation
- Exception handling with ImproperlyConfigured
- Caching via @lru_cache to improve performance
--
Ticket URL: <https://code.djangoproject.com/ticket/36604>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Sep 10, 2025, 9:33:30 PM (10 days ago) Sep 10
to django-...@googlegroups.com
#36604: Add utility for lazy model resolution by string label (e.g.
"app_label.ModelName")
------------------------------+----------------------------------------
Reporter: Mbulelo_Peyi | Owner: Mbulelo_Peyi
Type: New feature | Status: assigned
Component: Utilities | Version: 5.2
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 1
Needs tests: 1 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
------------------------------+----------------------------------------
Changes (by Mbulelo_Peyi):

* needs_docs: 0 => 1
* needs_tests: 0 => 1

--
Ticket URL: <https://code.djangoproject.com/ticket/36604#comment:1>

Django

unread,
Sep 17, 2025, 6:12:34 AM (4 days ago) Sep 17
to django-...@googlegroups.com
#36604: Add utility for lazy model resolution by string label (e.g.
"app_label.ModelName")
------------------------------+----------------------------------------
Reporter: Mbulelo_Peyi | Owner: Mbulelo_Peyi
Type: New feature | Status: closed
Component: Utilities | Version: 5.2
Severity: Normal | Resolution: wontfix
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 1
Needs tests: 1 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
------------------------------+----------------------------------------
Changes (by Sarah Boyce):

* resolution: => wontfix
* status: assigned => closed

Comment:

Thank you for your suggestion! When suggesting a new feature for Django,
the feature idea should first be proposed and discussed with the
community. To do that, please raise this on the [https://github.com/django
/new-features/issues ​new feature tracker].

I'll close the ticket for now, but if the community agrees with the
proposal, please return to this ticket and reference the forum discussion
so we can re-open it. For more information, please refer to
[https://docs.djangoproject.com/en/stable/internals/contributing/bugs-and-
features/#requesting-features the documented guidelines for requesting
features].
--
Ticket URL: <https://code.djangoproject.com/ticket/36604#comment:2>
Reply all
Reply to author
Forward
0 new messages