2011-04-24 50 views
0

我找到了答案!這發生了,如果我命名一個完全像應用程序名稱的腳本!python django ORM錯誤/錯誤?:...無法將關鍵字'id'解析爲字段。選擇是:id,

這是該帖子的更正(簡化)版本。 我試圖運行此腳本獨立(甚至位於應用程序目錄):

#!/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'] 

,並得到下面的錯誤。記住,腳本首先打印正確的請求結果到用戶配置,只有面臨的另一個錯誤打印不正確的錯誤描述:

# ./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 

# 
+0

您發佈U​​serProfile類嗎? – Jerzyk 2011-04-24 18:10:27

+0

錯誤是相同的尋址任何模型/類 – user722765 2011-04-25 14:01:43

+0

請用您的解決方案創建一個答案,而不是編輯問題。 – 2014-08-22 21:17:23

回答

0

我有同樣的問題上試圖從auth_group獲得第一組(Django的v 1.3。 5)

Group.objects.get(name='First Group')

給出了相同的FeildError

Stangerly這工作:

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 

我還沒挖成Django的代碼,以找出原因。

相關問題