Django:Model.objects.get因ImportError异常而失败

时间:2011-05-29 08:14:12

标签: python exception django-models importerror

我的模型代表我的系统中的设置,我在应用程序的另一部分使用它,以便导入有3个级别WORKING CODE <- Module <- Model

模型Variables



from django.db import models

class Variables(models.Model):
    key = models.CharField(max_length = 20, verbose_name = 'Variable')
    value = models.CharField(max_length = 1024)

    class Meta:
        app_label = 'core'

    def __unicode__(self):
        return '%s: %s' % (self.key, self.value,)

这是我在

中使用的代码

模块variables.py



from core.models.storage import Variables

def get_var(name):
  return Variables.objects.get(key = name)

模块config.py



  var = get_var('some_key')

当我使用django shell中的这些内容时,一切正常,但是当我调用get_var函数时,我ImportError异常

storage.py



from django.db import models

class Variables(models.Model):
    key = models.CharField(max_length = 20, verbose_name = 'Variable')
    value = models.CharField(max_length = 1024)

    class Meta:
        app_label = 'core'

    def __unicode__(self):
        return '%s: %s' % (self.key, self.value,)



  File "monitor_cli.py", line 19, in 
    print worker.get_provider()
  File "/home/sultan/Project/monitor/app/worker.py", line 14, in get_provider
    print Variables.objects.get(pk=1)
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/manager.py", line 132, in get
    return self.get_query_set().get(*args, **kwargs)
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 341, in get
    clone = self.filter(*args, **kwargs)
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 550, in filter
    return self._filter_or_exclude(False, *args, **kwargs)
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 568, in _filter_or_exclude
    clone.query.add_q(Q(*args, **kwargs))
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/query.py", line 1172, in add_q
    can_reuse=used_aliases, force_having=force_having)
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/query.py", line 1060, in add_filter
    negate=negate, process_extras=process_extras)
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/query.py", line 1226, in setup_joins
    field, model, direct, m2m = opts.get_field_by_name(name)
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/options.py", line 307, in get_field_by_name
    cache = self.init_name_map()
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/options.py", line 337, in init_name_map
    for f, model in self.get_all_related_m2m_objects_with_model():
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/options.py", line 414, in get_all_related_m2m_objects_with_model
    cache = self._fill_related_many_to_many_cache()
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/options.py", line 428, in _fill_related_many_to_many_cache
    for klass in get_models():
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/loading.py", line 167, in get_models
    self._populate()
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/loading.py", line 61, in _populate
    self.load_app(app_name, True)
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/loading.py", line 76, in load_app
    app_module = import_module(app_name)
  File "/usr/local/lib/python2.6/dist-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)
ImportError: No module named c

1 个答案:

答案 0 :(得分:0)

删除models目录,并将所有模型放在models.py文件中,除非你有充分的理由不这样做。将导入更改为from core.models import Variable(将Variables类重命名为Variable - django模型应命名为单数而非复数。

问题可能与您的模型位于models以外的名称空间中的事实有关;即。 models.storage。 django基础设施期望某些地方有某些东西。如果您要将模型放在与您正在执行的单独名称空间中,则应从__init__.py模块中的models文件导入它们。除非你有充分的理由,否则不要这样做。

最后,在提出这种性质的问题时,您应该提供更多信息。您应该显示所有相关文件的代码。您应该提供有关您所做的与django约定不同的详细信息(在本例中为models目录而不是models.py文件)。您还应该显示settings.py的相关设置,在本例中为INSTALLED_APPS设置。您还应该告诉我们您使用的是什么版本的django和python;这有时是相关的。预先提供的更多信息比信息量少得多。