models.py module is imported multiple times.
|Reported by:||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:2 Changed 3 years ago by
|Triage Stage:||Unreviewed → Accepted|
|Type:||Uncategorized → Cleanup/optimization|