0

我有一个测试视图,其中一个数据聚合查询引发了我不理解的错误。如果有人可以对此发表评论并指出正确的方向,我将不胜感激。这是我的第一个 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”的模型字段。有人可以解释我在这里做错了什么吗?

4

2 回答 2

0

在这里你没有提到任何主键,所以 django 自动创建了一个主键名称 id。在这里,您可以像这样设置主键:

group_id = models.DecimalField(max_digits=65535, decimal_places=65535, primary_key=True)

然后它将 group_id 作为主键。

于 2013-03-21T12:44:33.870 回答
0

看起来您正在尝试使用BusinessAreaToTeams. 与其让 group_id 和 team_id 成为包含原始 ID 的 DecimalFields,不如定义

group = ForeignKey(BusinessGroup)
team = ForeignKey(BusinessTeam)  #  guessing the model name here

这将在您的表中创建一个 group_id 和 team_id 字段,并允许您使用 django 的 QuerySets 轻松管理关系。请查看Django 书的第 10 章,了解有关该主题的介绍。

于 2013-03-21T11:29:36.827 回答