python - Django filter query - doesn't work -
i have problems django, want write simple query 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 trying (it should find in opinion):
>>> 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 wrote last line , hit enter 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 above exception, 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 above exception, 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 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 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 doing wrong, tried same .id doesn't work neither.
in django 1.7 , later can't use models without explicitly initialising django first. run following commands:
import django django.setup()
and after queries work.
relevant django doc: https://docs.djangoproject.com/en/dev/ref/applications/#initialization-process
Comments
Post a Comment