我的Django有问题,我想写一个非常简单的查询,但它不起作用。
型号:
class Games(models.Model):
name = models.CharField(max_length=128)
path_to_folder = models.CharField(max_length=256)
description = models.TextField()
cover = models.URLField()
def __str__(self):
return self.name
我正在尝试这样的事情(它应该在我看来发现):
>>> from gamepanel.models import Games
>>> e = Games.objects.all()
>>> print (e)
[<Games: Call Of Duty 4>]
>>> e[0].name
'Call Of Duty 4'
>>> q = Games.objects.filter(name=e[0].name)
但是当我写完最后一行并按下回车时我得到:
Traceback (most recent call last):
File "/home/zoli/Desktop/django-master/newest/django-trunk/django/db/models/options.py", line 404, in get_field_by_name
return self._name_map[name]
AttributeError: 'Options' object has no attribute '_name_map'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/zoli/Desktop/django-master/newest/django-trunk/django/db/models/options.py", line 518, in get_all_related_m2m_objects_with_model
cache = self._related_many_to_many_cache
AttributeError: 'Options' object has no attribute '_related_many_to_many_cache'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3.4/code.py", line 90, in runcode
exec(code, self.locals)
File "<input>", line 1, in <module>
File "/home/zoli/Desktop/django-master/newest/django-trunk/django/db/models/manager.py", line 80, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/zoli/Desktop/django-master/newest/django-trunk/django/db/models/query.py", line 702, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "/home/zoli/Desktop/django-master/newest/django-trunk/django/db/models/query.py", line 720, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "/home/zoli/Desktop/django-master/newest/django-trunk/django/db/models/sql/query.py", line 1316, in add_q
clause, require_inner = self._add_q(where_part, self.used_aliases)
File "/home/zoli/Desktop/django-master/newest/django-trunk/django/db/models/sql/query.py", line 1343, in _add_q
current_negated=current_negated, connector=connector)
File "/home/zoli/Desktop/django-master/newest/django-trunk/django/db/models/sql/query.py", line 1164, in build_filter
lookups, parts, reffed_aggregate = self.solve_lookup_type(arg)
File "/home/zoli/Desktop/django-master/newest/django-trunk/django/db/models/sql/query.py", line 1076, in solve_lookup_type
_, field, _, lookup_parts = self.names_to_path(lookup_splitted, self.get_meta())
File "/home/zoli/Desktop/django-master/newest/django-trunk/django/db/models/sql/query.py", line 1368, in names_to_path
field, model, direct, m2m = opts.get_field_by_name(name)
File "/home/zoli/Desktop/django-master/newest/django-trunk/django/db/models/options.py", line 406, in get_field_by_name
cache = self.init_name_map()
File "/home/zoli/Desktop/django-master/newest/django-trunk/django/db/models/options.py", line 435, in init_name_map
for f, model in self.get_all_related_m2m_objects_with_model():
File "/home/zoli/Desktop/django-master/newest/django-trunk/django/db/models/options.py", line 520, in get_all_related_m2m_objects_with_model
cache = self._fill_related_many_to_many_cache()
File "/home/zoli/Desktop/django-master/newest/django-trunk/django/db/models/options.py", line 534, in _fill_related_many_to_many_cache
for klass in self.apps.get_models():
File "/usr/lib/python3.4/functools.py", line 428, in wrapper
result = user_function(*args, **kwds)
File "/home/zoli/Desktop/django-master/newest/django-trunk/django/apps/registry.py", line 168, in get_models
self.check_models_ready()
File "/home/zoli/Desktop/django-master/newest/django-trunk/django/apps/registry.py", line 131, in check_models_ready
raise AppRegistryNotReady("Models aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady:模型尚未加载。
我不知道我做错了什么,我也试过用.id做同样的事情,但它既不起作用。
答案 0 :(得分:2)
在Django 1.7及更高版本中,你不能先使用模型而不先显式初始化Django。运行以下命令:
import django
django.setup()
之后,您的查询将有效。
相关的Django文档:https://docs.djangoproject.com/en/dev/ref/applications/#initialization-process