Django Query.get对象匹配查询不存在错误
请告诉我需要有关此代码的帮助:Django Query.get对象匹配查询不存在错误,django,django-models,Django,Django Models,请告诉我需要有关此代码的帮助: >>> t = Transaction.objects.filter(paid=True) >>> t [<Transaction: ac0e95f6cd994cc39807d986f7a10d4d>, <Transaction: 7067361871fd459f aa144988ffa22c7c>, <Transaction: 134e5ab4b0a74b5a985ff53e31370818>
>>> t = Transaction.objects.filter(paid=True)
>>> t
[<Transaction: ac0e95f6cd994cc39807d986f7a10d4d>, <Transaction: 7067361871fd459f
aa144988ffa22c7c>, <Transaction: 134e5ab4b0a74b5a985ff53e31370818>, <Transaction
: ef451670efad4995bff755621c162807>]
>>> t[0]
<Transaction: ac0e95f6cd994cc39807d986f7a10d4d>
>>> t[0].branch_name
<Branch: WAREHOUSE ROAD>
>>> Transaction.objects.get(branch_name='WAREHOUSE ROAD')
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "C:\Python27\lib\site-packages\django-1.3-py2.7.egg\django\db\models\mana
ger.py", line 132, in get
return self.get_query_set().get(*args, **kwargs)
File "C:\Python27\lib\site-packages\django-1.3-py2.7.egg\django\db\models\quer
y.py", line 344, in get
num = len(clone)
File "C:\Python27\lib\site-packages\django-1.3-py2.7.egg\django\db\models\quer
y.py", line 82, in __len__
self._result_cache = list(self.iterator())
File "C:\Python27\lib\site-packages\django-1.3-py2.7.egg\django\db\models\quer
y.py", line 273, in iterator
for row in compiler.results_iter():
File "C:\Python27\lib\site-packages\django-1.3-py2.7.egg\django\db\models\sql\
compiler.py", line 680, in results_iter
for rows in self.execute_sql(MULTI):
File "C:\Python27\lib\site-packages\django-1.3-py2.7.egg\django\db\models\sql\
compiler.py", line 735, in execute_sql
cursor.execute(sql, params)
File "C:\Python27\lib\site-packages\django-1.3-py2.7.egg\django\db\backends\ut
il.py", line 34, in execute
return self.cursor.execute(sql, params)
File "C:\Python27\lib\site-packages\django-1.3-py2.7.egg\django\db\backends\my
sql\base.py", line 86, in execute
return self.cursor.execute(query, args)
File "build\bdist.win32\egg\MySQLdb\cursors.py", line 176, in execute
if not self._defer_warnings: self._warning_check()
File "build\bdist.win32\egg\MySQLdb\cursors.py", line 92, in _warning_check
warn(w[-1], self.Warning, 3)
Warning: Truncated incorrect DOUBLE value: 'WAREHOUSE ROAD'
我尝试按如下方式序列化事务:
>>> from django.core import serializers
>>> serializers.serialize('csv', t)
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "C:\Python27\lib\site-packages\django-1.3-py2.7.egg\django\core\serialize
rs\__init__.py", line 91, in serialize
s.serialize(queryset, **options)
File "C:\Python27\lib\site-packages\django-1.3-py2.7.egg\django\core\serialize
rs\base.py", line 48, in serialize
self.handle_fk_field(obj, field)
File "C:\Python27\lib\site-packages\django-1.3-py2.7.egg\django\core\serialize
rs\python.py", line 48, in handle_fk_field
related = getattr(obj, field.name)
File "C:\Python27\lib\site-packages\django-1.3-py2.7.egg\django\db\models\fiel
ds\related.py", line 315, in __get__
rel_obj = QuerySet(self.field.rel.to).using(db).get(**params)
File "C:\Python27\lib\site-packages\django-1.3-py2.7.egg\django\db\models\quer
y.py", line 349, in get
% self.model._meta.object_name)
DoesNotExist: Branch matching query does not exist.
来自django.core导入序列化程序的>
>>>序列化器。序列化('csv',t)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“C:\Python27\lib\site packages\django-1.3-py2.7.egg\django\core\serialize
rs\\uuuu init\uuuuu.py”,第91行,序列化
s、 序列化(查询集,**选项)
文件“C:\Python27\lib\site packages\django-1.3-py2.7.egg\django\core\serialize
rs\base.py”,第48行,序列化
self.handle\u fk\u字段(对象,字段)
文件“C:\Python27\lib\site packages\django-1.3-py2.7.egg\django\core\serialize
rs\python.py”,第48行,在handle\u fk\u字段中
related=getattr(对象,字段名)
文件“C:\Python27\lib\site packages\django-1.3-py2.7.egg\django\db\models\fiel
ds\related.py”,第315行,在__
rel_obj=QuerySet(self.field.rel.to)。使用(db.get(**参数)
文件“C:\Python27\lib\site packages\django-1.3-py2.7.egg\django\db\models\query”
y、 py”,第349行,在get中
%self.model.\u meta.object\u name)
DoesNotExist:分支匹配查询不存在。
我不明白为什么get会在分支上返回DoesNotExists。我展示了上面的一个示例,该示例显示分支在事务中有一个记录
循环通过t我得到一个结果,但随后是DoesNotExist:分支匹配查询不存在
>>> for i in t:
... i.branch_name
...
<Branch: WAREHOUSE ROAD>
Traceback (most recent call last):
File "<console>", line 2, in <module>
File "C:\Python27\lib\site-packages\django-1.3-py2.7.egg\django\db\models\fiel
ds\related.py", line 315, in __get__
rel_obj = QuerySet(self.field.rel.to).using(db).get(**params)
File "C:\Python27\lib\site-packages\django-1.3-py2.7.egg\django\db\models\quer
y.py", line 349, in get
% self.model._meta.object_name)
DoesNotExist: Branch matching query does not exist.
>>对于i in t:
... i、 分行名称
...
回溯(最近一次呼叫最后一次):
文件“”,第2行,在
文件“C:\Python27\lib\site packages\django-1.3-py2.7.egg\django\db\models\fiel
ds\related.py”,第315行,在__
rel_obj=QuerySet(self.field.rel.to)。使用(db.get(**参数)
文件“C:\Python27\lib\site packages\django-1.3-py2.7.egg\django\db\models\query”
y、 py”,第349行,在get中
%self.model.\u meta.object\u name)
DoesNotExist:分支匹配查询不存在。
请帮忙。谢谢此查询:
Transaction.objects.get(branch_name='WAREHOUSE ROAD')
为分支机构名称
筛选,分支机构名称是外键
字段。要查询该名称,应使用两个下划线:
Transaction.objects.get(branch_name__name_branch='WAREHOUSE ROAD')
对你的字段来说,这不是最方便的名称…顺便问一下:你上一段代码中的
t
是什么?也就是说,t=Transaction.objects.filter(paid=True)谢谢。它使我更接近了一步。但是在t=Transaction.objects.filter(paid=True)
上需要真正的帮助。分支是“t”结果的一部分。我不是直接在代码中调用Transaction.objects.get(branch_name='WAREHOUSE ROAD')。我只是把它当作一个测试,看看能得到什么回报。您能否帮助解释为什么serializers.serialize('csv',t)
wheret=Transaction.objects.filter(paid=True)
正在给出DoesNotExist:分支匹配查询不存在。
谢谢我的回答并不能解决这个问题:您的事务
对象似乎指向一个不存在的分支
。t.branch\u name
为您提供了什么?>>t.branch\u name回溯(最近一次调用):文件“”,第1行,在AttributeError中:“QuerySet”对象没有属性“branch\u name”
对不起,我移动了我的注释:)。我还发布了错误的请求,因为您的t
对象当然是QuerySet
。无论如何,我仍然认为在QuerySet
中有一个事务
,它指向一个无效的分支。要检查哪个,请尝试以下操作:在t:\nprint t.branch\u name
中为i设置()(当然格式正确。如果捕获到异常,您可能可以调试它以找到错误的事务
对象。我已编辑帖子以反映循环。显示它不是无效的分支,并且异常相同。
Transaction.objects.get(branch_name__name_branch='WAREHOUSE ROAD')