I Am trying to refactor my code for a series of Django models, but this is valid for general python imports. Let me explain my setup and problem (with completly fake classes etc)
APP_ROOT/
|- __init__.py
|- tests/
|- urls.py
|- models/
| |- __init__.py
| |- BunchOfModels.py
| |- SomeMoreModels.py
\- admin.py
in models.__init__:
# models/__init__.py
from APP_ROOT.models.BunchOfModels import *
from APP_ROOT.models.SomeMoreModels import *
this allows me to do the following:
from APP_ROOT.models import SuperModel
# Where SuperModel is in SomeMoreModels
The problem is that the class repr is:
<class 'project.APP_ROOT.models.SomeMoreModels.SuperModel'>
which messes up the foreign keys.
my question:
Is there a way to do this so that all my classes have a repr more like:
<class 'project.APP_ROOT.models.SuperModel'> without manually overloading the __repr__ for each class. I’m not even sure that this would solve the problem, and even if it did, This wouldn’t port very well.
or do i need to go back to using one big, ugly file…
As I already commented, Django doesn’t recommend you to spread your models on several files. But this can solve your problem
app_labelattr should be inMetaclass inside every model. You also can inheritMetafrom base metaclass.Other approach could be moving managers, utility function outside
models.py(if you have them). But in this case you likely will get circular import.