问题
I have problems with my Django, I want to write a very simple query but it doesn't work.
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.
I have no idea what I am doing wrong, I also tried do the same with .id but it doesn't work neither.
回答1:
In Django 1.7 and later you can't use models without explicitly initialising Django first. 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
来源:https://stackoverflow.com/questions/25353959/django-filter-query-doesnt-work