我正在编写一个Django应用程序,它从Bugzilla数据库中提取数据,但我无法获取标记。
class Bugzilla_bugs(models.Model):
class Meta:
db_table = 'bugs'
bug_id = models.IntegerField(primary_key=True)
...
flagtypes表,描述了所有不同的标志名称及其含义。
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和status值的一对多映射。
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'字段的值。
如果我尝试使用flaglines来查询数字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)
我做错了什么?
答案 0 :(得分:1)
好的,我现在看到,我认为问题是你的标志模型定义为:
type_id = models.ForeignKey(Bugzilla_flagtypes,related_name='flagtype',db_column='type_id',to_field='name')`
...但是在数据库中,type_id
表中的flags
列明确包含了flagtype的整数id
...换句话说,您的模型定义没有&#39; t匹配数据库模式,您需要从上面的ForeignKey中删除to_field
。
然后这应该有效:
bug.flaglines.get(type_id__name="UnlocksBranch")