我有一个Django模型,我正在导入一些项目:
from django.db import models
from mcif.models.import_profile import ImportProfile
from mcif.models.import_file import ImportFile
from mcif.models.import_bundle import ImportBundle
from mcif.models.customer import Customer
#from mcif.models.account_import import AccountImport
from mcif.models.csv_row import CSVRow
import csv, cStringIO
它工作正常,但是当我取消评论该评论的那一行时,我得到了这个:
Traceback (most recent call last):
File "./manage.py", line 11, in <module>
execute_manager(settings)
File "/usr/lib/pymodules/python2.6/django/core/management/__init__.py", line 438, in execute_manager
utility.execute()
File "/usr/lib/pymodules/python2.6/django/core/management/__init__.py", line 379, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/lib/pymodules/python2.6/django/core/management/base.py", line 191, in run_from_argv
self.execute(*args, **options.__dict__)
File "/usr/lib/pymodules/python2.6/django/core/management/base.py", line 220, in execute
output = self.handle(*args, **options)
File "/usr/lib/pymodules/python2.6/django/core/management/base.py", line 351, in handle
return self.handle_noargs(**options)
File "/usr/lib/pymodules/python2.6/django/core/management/commands/shell.py", line 18, in handle_noargs
loaded_models = get_models()
File "/usr/lib/pymodules/python2.6/django/db/models/loading.py", line 167, in get_models
self._populate()
File "/usr/lib/pymodules/python2.6/django/db/models/loading.py", line 64, in _populate
self.load_app(app_name)
File "/usr/lib/pymodules/python2.6/django/db/models/loading.py", line 78, in load_app
models = import_module('.models', app_name)
File "/usr/lib/pymodules/python2.6/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/home/jason/projects/mcifdjango/mcif/models/__init__.py", line 5, in <module>
from mcif.models.account_import import AccountImport
File "/home/jason/projects/mcifdjango/mcif/models/account_import.py", line 2, in <module>
from mcif.models.generic_import import GenericImport
File "/home/jason/projects/mcifdjango/mcif/models/generic_import.py", line 6, in <module>
from mcif.models.account_import import AccountImport
ImportError: cannot import name AccountImport
为什么Django不喜欢这个特定的文件?
(我可以在控制台上自己加载AccountImport
就好了。)
此外,如果看到它有AccountImport
:
from django.db import models
from mcif.models.generic_import import GenericImport
class AccountImport(models.Model):
id = models.BigIntegerField(primary_key=True)
generic_import = models.ForeignKey(GenericImport)
is_entirely_international = models.IntegerField()
is_queued = models.IntegerField()
created_at = models.DateTimeField()
updated_at = models.DateTimeField()
class Meta:
db_table = u'account_import'
app_name = 'mcif'
GenericImport
:
from django.db import models
from mcif.models.import_profile import ImportProfile
from mcif.models.import_file import ImportFile
from mcif.models.import_bundle import ImportBundle
from mcif.models.customer import Customer
from mcif.models.csv_row import CSVRow
import csv, cStringIO
class GenericImport(models.Model):
class Meta:
db_table = u'generic_import'
app_name = 'mcif'
id = models.BigIntegerField(primary_key=True)
import_profile = models.ForeignKey(ImportProfile)
import_file = models.ForeignKey(ImportFile)
notes = models.TextField()
start_time = models.DateTimeField()
end_time = models.DateTimeField()
active = models.IntegerField()
created_at = models.DateTimeField()
updated_at = models.DateTimeField()
unsavable_rows = models.TextField()
import_bundle = models.ForeignKey(ImportBundle)
is_queued = models.IntegerField()
@classmethod
def last(cls):
all = GenericImport.objects.all()
return all[len(all) - 1]
def process(self):
for line in self.import_file.file.split("\n")[:30]:
f = cStringIO.StringIO(line)
row = CSVRow()
row.array = next(csv.reader(f))
row.generic_import = self
row.process()
f.close()
def specific_import(self):
for model_name in ['TransactionImport', 'AccountImport']:
specific_imports = eval(model_name + '.objects.filter(generic_import__pk=5)')
if len(specific_imports) > 0:
return specific_imports[0]
return False
答案 0 :(得分:2)
您有循环导入 - mcif.models.generic_import
和mcif.models.account_import
正试图互相导入。
请记住,Python不是Java,并且非常乐意在一个文件中包含多个类,特别是如果它们像这两个类似的密切相关。将它们放在一个mcif.models
文件中。
答案 1 :(得分:2)
这是循环模型依赖。
File "/home/jason/projects/mcifdjango/mcif/models/__init__.py", line 5, in <module>
from mcif.models.account_import import AccountImport
File "/home/jason/projects/mcifdjango/mcif/models/account_import.py", line 2, in <module>
from mcif.models.generic_import import GenericImport
File "/home/jason/projects/mcifdjango/mcif/models/generic_import.py", line 6, in <module>
from mcif.models.account_import import AccountImport
ImportError: cannot import name AccountImport
如果您确实需要将这些模型分开,请查看http://docs.djangoproject.com/en/1.2/ref/models/fields/#django.db.models.ForeignKey以获得解决方案。您可以使用字符串来引用模型,而不是导入模型。
class Car(models.Model):
manufacturer = models.ForeignKey('production.Manufacturer')
将使用Manufacturer
模块中的production
模型作为外键的目标。
答案 2 :(得分:0)
看起来你在那里有一个循环导入。
File "/home/jason/projects/mcifdjango/mcif/models/__init__.py", line 5, in <module>
from mcif.models.account_import import AccountImport
File "/home/jason/projects/mcifdjango/mcif/models/account_import.py", line 2, in <module>
from mcif.models.generic_import import GenericImport
File "/home/jason/projects/mcifdjango/mcif/models/generic_import.py", line 6, in <module>
from mcif.models.account_import import AccountImport
导入__init__.py
的{{1}}导入AccountImport
然后再次导入GenericImport
,但我不确定如何导入它根本就可以正常工作。
您的AccountImport
确实需要导入GenericImport
吗?看起来像破碎的等级。
修改 :(用于更新)
AccountImport
def specific_import(self):
for model_name in ['TransactionImport', 'AccountImport']:
specific_imports = eval(model_name + '.objects.filter(generic_import__pk=5)')
if len(specific_imports) > 0:
return specific_imports[0]
return False
行不良行为。这里肯定有比这更好的模式。您的通用类不应该对您的具体实现有任何了解或依赖,更不用说eval
在实际代码中几乎没有任何地方。