我有一个测试视图,其中一个数据聚合查询引发了我不理解的错误。如果有人可以对此发表评论并指出正确的方向,我将不胜感激。这是我的第一个 django 项目,我正在经历学习曲线,所以请多多包涵:
代码:
biz_group = BusinessGroup.objects.get(group_manager=user)
group_team = BusinessGroupToTeams.objects.get(group_id=biz_group.group_id)
引发错误:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/django/db/models/manager.py", line 143, in get
return self.get_query_set().get(*args, **kwargs)
File "/usr/lib/python2.7/dist-packages/django/db/models/query.py", line 394, in get
num = len(clone)
File "/usr/lib/python2.7/dist-packages/django/db/models/query.py", line 90, in __len__
self._result_cache = list(self.iterator())
File "/usr/lib/python2.7/dist-packages/django/db/models/query.py", line 301, in iterator
for row in compiler.results_iter():
File "/usr/lib/python2.7/dist-packages/django/db/models/sql/compiler.py", line 775, in results_iter
for rows in self.execute_sql(MULTI):
File "/usr/lib/python2.7/dist-packages/django/db/models/sql/compiler.py", line 840, in execute_sql
cursor.execute(sql, params)
File "/usr/lib/python2.7/dist-packages/django/db/backends/util.py", line 41, in execute
return self.cursor.execute(sql, params)
File "/usr/lib/python2.7/dist-packages/django/db/backends/postgresql_psycopg2/base.py", line 58, in execute
six.reraise(utils.DatabaseError, utils.DatabaseError(*tuple(e.args)), sys.exc_info()[2])
File "/usr/lib/python2.7/dist-packages/django/db/backends/postgresql_psycopg2/base.py", line 54, in execute
return self.cursor.execute(query, args)
DatabaseError: column businessgroup_to_teams.id does not exist
LINE 1: SELECT "businessgroup_to_teams"."id", "businessgroup_to_teams"...
BusinessGroupToTeams 模型如下所示:
class BusinessAreaToTeams(models.Model):
group_id = models.DecimalField(max_digits=65535, decimal_places=65535)
team_id = models.DecimalField(max_digits=65535, decimal_places=65535)
class Meta:
db_table = 'businessgroup_to_teams'
我没有查询“id”,也没有“id”的模型字段。有人可以解释我在这里做错了什么吗?