简体   繁体   English

Django过滤器查询-不起作用

[英]Django filter query - doesn't work

I have problems with my Django, I want to write a very simple query but it doesn't work. 我的Django出现问题,我想编写一个非常简单的查询,但它不起作用。

Model: 模型:

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

I am trying something like this (It should find itself in my opinion): 我正在尝试这样的事情(我认为它应该会发现自己):

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

but when I wrote the last line and hit enter I get: 但是当我写最后一行并按回车时,我得到:

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: Models aren't loaded yet. django.core.exceptions.AppRegistryNotReady:模型尚未加载。

I have no idea what I am doing wrong, I also tried do the same with .id but it doesn't work neither. 我不知道我在做什么错,我也尝试用.id做同样的事情,但是它都不起作用。

In Django 1.7 and later you can't use models without explicitly initialising Django first. 在Django 1.7及更高版本中,您必须先明确初始化Django才能使用模型。 Run the following commands: 运行以下命令:

import django
django.setup()

And after that your queries will work. 之后,您的查询将起作用。

Relevant Django doc: https://docs.djangoproject.com/en/dev/ref/applications/#initialization-process 相关的Django文档: https : //docs.djangoproject.com/en/dev/ref/applications/#initialization-process

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM