First of all thanks for all responses.
I've still one more question which is connected to that one particular
issue. What if classes are importing each other.
models/
__init__.py
one.py
two.py
====== __init__.py =======
from one import ModelOne
from two import ModelTwo
======= one.py ========
from django.db import models
from two import ModelTwo
class ModelOne(models.Model):
two = models.ForeignKey('ModelTwo')
class Meta:
app_label = 'appname'
def all_one(self):
return ModelTwo.objects.all()
======= two.py ========
from django.db import models
from one import ModelOne
class ModelTwo(models.Model):
one = models.ForeignKey('ModelOne')
class Meta:
app_label = 'appname'
def all_two(self):
return ModelOne.objects.all()
In case above appears problem with cyclic import. I know that example
is a bit silly but I'm trying to use it in real project files with
more than 1000 lines ~300 per class.
==== Traceback ====
./manage.py validate
Traceback (most recent call last):
File "./manage.py", line 11, in <module>
execute_manager(settings)
File "/var/lib/python-support/python2.5/django/core/management/
__init__.py", line 340, in execute_manager
utility.execute()
File "/var/lib/python-support/python2.5/django/core/management/
__init__.py", line 295, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/var/lib/python-support/python2.5/django/core/management/
base.py", line 192, in run_from_argv
self.execute(*args, **options.__dict__)
File "/var/lib/python-support/python2.5/django/core/management/
base.py", line 219, in execute
output = self.handle(*args, **options)
File "/var/lib/python-support/python2.5/django/core/management/
base.py", line 348, in handle
return self.handle_noargs(**options)
File "/var/lib/python-support/python2.5/django/core/management/
commands/validate.py", line 9, in handle_noargs
self.validate(display_num_errors=True)
File "/var/lib/python-support/python2.5/django/core/management/
base.py", line 246, in validate
num_errors = get_validation_errors(s, app)
File "/var/lib/python-support/python2.5/django/core/management/
validation.py", line 28, in get_validation_errors
for (app_name, error) in get_app_errors().items():
File "/var/lib/python-support/python2.5/django/db/models/
loading.py", line 128, in get_app_errors
self._populate()
File "/var/lib/python-support/python2.5/django/db/models/
loading.py", line 57, in _populate
self.load_app(app_name, True)
File "/var/lib/python-support/python2.5/django/db/models/
loading.py", line 72, in load_app
mod = __import__(app_name, {}, {}, ['models'])
File "/.../appname/models/__init__.py", line 1, in <module>
from one import ModelOne
File "/.../appname/models/one.py", line 2, in <module>
from two import ModelTwo
File "/.../appname/distmodels/models/two.py", line 2, in <module>
from one import ModelOne
ImportError: cannot import name ModelOne
Regards
x_O