Answer the question
In order to leave comments, you need to log in
How to get rid of large models.py?
Faced the problem of rapidly growing models.py on django, about 5t lines in total, it became necessary to split it into different files. I split the pieces into 6, according to the documentation I made a package with __init__.py, all imports are fine there. But these models are very closely related to each other, many use a class of models from other files, hence a large number of imports between them and a circular import error, I can’t imagine how this can be fixed. It seems to me that the only way out is to call apps.get_model () every time inside each method, but it sounds too crooked.
Answer the question
In order to leave comments, you need to log in
To avoid circular imports, either use apps.get_model() as you mentioned, or import the whole module, not specific models. That is, not ,
and
then refer to the model in the code through
from somemodule.models import SomeModel
import somemodule.models as somemodule_models
somemodule_models.SomeModel.objects(...)...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question