开发者

Django: Model.objects.get fails with ImportError exception

开发者 https://www.devze.com 2023-03-09 14:21 出处:网络
I\'ve model which represents settings in my system and I use it from another part of my application so that import has 3 levels WORKING CODE <- Module <- Model

I've model which represents settings in my system and I use it from another part of my application so that import has 3 levels WORKING CODE <- Module <- Model

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,)

Here is the code I'm using it from

Module variables.py



from core.models.storage import Variables

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

Module config.py



  var = get_var('some_key')

When I use this stuff from django shell everything works well but when I call get_var function I've ImportError exception

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开发者_如何学Python "/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


Get rid of the models directory, and put all your models in a models.py file, unless you have a VERY good reason not to. Change your imports to from core.models import Variable (rename your Variables class to Variable - django models should be named as the singular not the plural).

The problem probably has to do with the fact that your models live in namespaces other than models; ie. models.storage. The django infrastructure expects certain things to be in certain places. If you're going to put models in separate namespaces like you're doing, you should be importing them from the __init__.py file within the models module. Don't do this, again, unless you have a very good reason.

Finally, when asking questions of this nature, you should provide a lot more information. You should show the code of all of the relevant files. You should provide detail on what you've done that deviates from the convention of django (in this case, a models directory rather than a models.py file). You should also show relevant settings from settings.py, in this case, your INSTALLED_APPS setting. You should also tell us what version of django and python you are using; this is sometimes relevant. More information upfront is much better than less information.

0

精彩评论

暂无评论...
验证码 换一张
取 消