Model importing race condition created in  for subclasses of abstract classes that have fields.
|Reported by:||bretthoerner||Owned by:||nobody|
|Cc:||adulic@…, safariman@…, davisd.davisd@…, rokclimb15@…||Triage Stage:||Accepted|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
We ran into a weird edge case race condition that seems to be caused by . FWIW I haven't taken all the time needed to understand exactly why or what  does, I'm just reporting the error based on a traceback and example code set. :)
Basic things you need:
- An abstract base class with a field, and a model A that uses it, so that ModelBase.__new__ will drop into the "new_class.add_to_class(field.name, copy.deepcopy(field))" branch intended for abstract only.
- Another model (in a different module) that imports model A.
- A management command that imports model A (this won't happen in the shell, I'm not sure what the difference is yet or where else it would occur).
Basically it seems that the new "self.default = self.model._meta.get_field_by_name(self.name).default" results eventually in a "get_models()", but this is run *while model A is being loaded* which means that all models are fetched, including the models file which contains model B and thus imports model A, and then blammo, circular dependency problem.
I've attached a simple project with two apps and a sample management command.
Change History (13)
Changed 5 years ago by bretthoerner
comment:1 Changed 5 years ago by ramiro
- Needs documentation unset
- Needs tests unset
- Patch needs improvement unset
comment:2 Changed 5 years ago by russellm
- milestone set to 1.2
- Triage Stage changed from Unreviewed to Accepted
Changed 5 years ago by erik
comment:10 Changed 5 years ago by russellm
- Resolution set to fixed
- Status changed from new to closed