Model importing race condition created in  for subclasses of abstract classes that have fields.
|Reported by:||Brett Hoerner||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)
comment:1 Changed 7 years ago by
|Patch needs improvement:||unset|