Opened 11 years ago
Closed 11 years ago
#21681 closed Cleanup/optimization (fixed)
Simplify Apps.populate_models
Reported by: | Aymeric Augustin | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | Normal | Keywords: | app-loading |
Cc: | Triage Stage: | Accepted | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
There are two highly dubious things in the current implementation of the app registry (which date back to a refactoring by Malcolm 7 years ago):
1) The "postponing" behavior which is designed to avoid circular imports when populate_models() is triggered during the import of a models module. Depending on the implementation of #21676, that may become unnecessary. ptone once suggested that #18251 removed the need for this behavior, but I'm not sure why.
2) The three uses of get_registered_model. It looks like it's optional. But ModelBase.new is scary...
Change History (9)
comment:1 by , 11 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:2 by , 11 years ago
comment:3 by , 11 years ago
Another related issue: the current implementation of populate_models() isn't exactly idempotent. A recursive invocation may return while models aren't fully populated, and it won't set _models_loaded. This could probably be an issue in some cases, but it would be moot if we got rid of the postponing.
comment:4 by , 11 years ago
Regarding the 2nd point, here's an... interesting... behavior.
# myapp/models.py from django.db import models class MyModel(models.Model): pass first_model_class = MyModel # save a reference class MyModel(models.Model): pass second_model_class = MyModel # save a reference if second_model_class is first_model_class: raise RuntimeError("WTF")
Thank you https://github.com/django/django/blob/efddae25/django/db/models/base.py#L153-L156.
comment:5 by , 11 years ago
This behavior was added without a test in c63dcdda372d4d8a37abd39432deb62b8d4b1d23 to fix #1796.
While the example I'm showing is obviously an unintended consequence, we should proceed carefully.
comment:6 by , 11 years ago
Version: | 1.6 → master |
---|
comment:9 by , 11 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
This issue is fixed by the two commits above. The part about ModelBase
was moved to #21711.
See also https://github.com/django/django/pull/2089#discussion_r8516536.