【发布时间】:2015-03-16 07:05:44
【问题描述】:
我正在编写一个从 Bugzilla 数据库中提取数据的 Django 应用程序,但在获取标志时遇到了麻烦。
class Bugzilla_bugs(models.Model):
class Meta:
db_table = 'bugs'
bug_id = models.IntegerField(primary_key=True)
...
描述所有不同标志名称及其含义的标志类型表。
class Bugzilla_flagtypes(models.Model):
class Meta:
db_table = 'flagtypes'
name = models.CharField(max_length=50,unique=True)
description = models.TextField()
...
flags 表,它是每个 bug 的 bug_id、type_id 和状态值的一对多映射。
class Bugzilla_flags(models.Model):
class Meta:
db_table = 'flags'
type_id = models.ForeignKey(Bugzilla_flagtypes,related_name='flagtype',db_column='type_id',to_field='name')
status = models.CharField(max_length=50)
bug_id = models.ForeignKey(Bugzilla_bugs,related_name='flaglines',db_column='bug_id',to_field='bug_id')
当我尝试获取特定错误的标志时:
bug = Bugzilla_bugs.objects.using('bugzilla').get(bug_id=12345)
bug.flaglines.get(type_id="UnlocksBranch")
我得到了例外:
>>> bug.flaglines.get(type_id__name="UnlocksBranch")
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/django/db/models/manager.py", line 92, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 351, in get
num = len(clone)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 122, in __len__
self._fetch_all()
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 966, in _fetch_all
self._result_cache = list(self.iterator())
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 265, in iterator
for row in compiler.results_iter():
File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.py", line 700, in results_iter
for rows in self.execute_sql(MULTI):
File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.py", line 786, in execute_sql
cursor.execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 81, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/mysql/base.py", line 128, in execute
return self.cursor.execute(query, args)
File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 176, in execute
if not self._defer_warnings: self._warning_check()
File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 92, in _warning_check
warn(w[-1], self.Warning, 3)
Warning: Incorrect integer value: 'UnlocksBranch' for column 'type_id' at row 1
我正在尝试使用 'get' 方法来获取 flags 表中的 'status' 字段的值。
如果我尝试使用标志线来查询数字 type_id,我会得到 DoesNotExist。
>>> bug.flaglines.get(type_id=20)
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 460, in __repr__
u = six.text_type(self)
File "/home/shubbard/django/cpe/dev/cpe/bugzilla/models.py", line 160, in __unicode__
return unicode(self.type_id)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/related.py", line 572, in __get__
rel_obj = qs.get()
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 357, in get
self.model._meta.object_name)
DoesNotExist: Bugzilla_flagtypes matching query does not exist.
我知道该错误设置了该标志。
mysql> select * from flags where bug_id=12345;
+-------+---------+--------+--------+
| id | type_id | status | bug_id |
+-------+---------+--------+--------+
| 71732 | 29 | + | 12345 |
| 72538 | 41 | + | 12345 |
| 72547 | 12 | + | 12345 |
| 72548 | 31 | ? | 12345 |
| 72549 | 33 | ? | 12345 |
| 72550 | 20 | ? | 12345 |
| 72551 | 36 | ? | 12345 |
+-------+---------+--------+--------+
7 rows in set (0.01 sec)
我做错了什么?
【问题讨论】:
-
没错,你的
bug_id应该是一个整数,也就是一个整数。UnlocksBranch不是一个数字,除非它可能在 Base-36 数字系统中。 -
bug_id 是一个整数,12345。如果我查看数据库中的行,bug_id 字段是 12345,type_id 是 20,状态是“?”。在 flagtypes 表中,行 ID 20 的名称为“UnlocksBranch”。我想得到的是“?”的状态。
-
bug.flaglines.get(type_id="UnlocksBranch")20),而不是 flagtype 名称 -
如果我搜索 type_id 的编号,我得到:DoesNotExist: Bugzilla_flagtypes 匹配查询不存在。我知道该行确实存在,并且 flags 表中有一行与匹配的 bug_id 和 type_id 在同一行中。
-
你需要使用 type_id(你说的是
20)而不是 bug_id12345