python django ORM error/bug?: ...Cannot resolve keyword 'id' into field. Choices are: id,
i found the answer! this happened if i named a script exactly like the app name!
this is a corrected (simplified) version of the post. i'm trying to run this standalone script (or even located in the app directory):
#!/usr/local/bin/python -W ignore
# coding: utf-8
import sys, os
sys.path.append('/usr/home/code')
os.environ['DJANGO_SETTINGS_MODULE'] = 'tuppy.settings'
from tuppy.tup.models import *
some_dict={}
print UserProfile.objects.filter(id=1)
print 'lallala'
print some_dict['unexisting_key']
and get the following error. mind that the script first prints the correct request result to UserProfile, and only facing another error prints incorrect error description:
# ./tup.py
[<UserProfile: 115>]
lallala
Traceback (most recent call last):
File "./tup.py", line 10, in <module>
p = UserProfile.objects.filter(id=1)
File 开发者_运维百科"/usr/local/lib/python2.6/site-packages/django/db/models/manager.py", line 141, in filter
return self.get_query_set().filter(*args, **kwargs)
File "/usr/local/lib/python2.6/site-packages/django/db/models/query.py", line 556, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "/usr/local/lib/python2.6/site-packages/django/db/models/query.py", line 574, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "/usr/local/lib/python2.6/site-packages/django/db/models/sql/query.py", line 1152, in add_q
can_reuse=used_aliases)
File "/usr/local/lib/python2.6/site-packages/django/db/models/sql/query.py", line 1045, in add_filter
negate=negate, process_extras=process_extras)
File "/usr/local/lib/python2.6/site-packages/django/db/models/sql/query.py", line 1215, in setup_joins
"Choices are: %s" % (name, ", ".join(names)))
django.core.exceptions.FieldError: Cannot resolve keyword 'id' into field. Choices are: credit_limit, id, insured_order_limit, mob_tel, resale_limit, sec_tel, status, user, voice_psw
#
I had the same issue on trying to get the first group from auth_group (Django v. 1.3.5)
Group.objects.get(name='First Group')
gave the same FeildError
.
Stangerly this worked:
try:
Group.objects.get(name="Active Rater") #crazily not working
except django.core.exceptions.FieldError as e:
group = Group.objects.get(name="Active Rater") #crazily works
I have not yet dug into the django code to figure out why.
精彩评论