models.py module is imported multiple times.
|Reported by:||gwahl@…||Owned by:||nobody|
|Component:||Database layer (models, ORM)||Version:||1.5|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
When a models.py module raises an ImportError for whatever reason, Django will attempt to import it many times. This can be demonstrated thus:
# models.py print 'loaded' import nonexistent_module
'loaded' will be printed more than once. This is due to the 'postponing' behavior during model loading: https://github.com/django/django/blob/master/django/db/models/loading.py#L118.
This is an issue for me because I have a registry system that tracks models and only allows them to be registered once.
class WhateverModel(models.Model): pass registry.register(WhateverModel) # checks that models are only registered once import nonexistent_module # oops, now it's registered twice (or more)
Change History (4)
comment:1 Changed 2 years ago by CollinAnderson
- Needs documentation unset
- Needs tests unset
- Patch needs improvement unset
comment:2 Changed 2 years ago by wim@…
- Triage Stage changed from Unreviewed to Accepted
- Type changed from Uncategorized to Cleanup/optimization
comment:3 Changed 23 months ago by aaugustin
- Component changed from Uncategorized to Database layer (models, ORM)