Django“where子句中的未知列”用于排除,但不过滤

时间:2014-04-06 01:06:44

标签: django django-models

我有一个Sale模型,其中一个外键是一个Prints模型,带有一个衬衫模型的外键,最后是一个Style模型的外键。在最后一个模型中,有一个ptype字段,它是另一个模型的外键。

这些列都存在于mysql表中(它已经使用了一段时间,但我仍然检查了四倍)。

这是我的代码:

sales = sales.exclude(print__shirt__style__ptype=ptype)

它给了我这个错误:

(1054, "Unknown column 'appname_prints.ptype_id' in 'where clause'")

请注意,它在print模型中而不是在样式模型中查找ptype_id字段。为什么忽略' __ 衬衫_ _style'?

样式模型还有一个品牌外键。我尝试用品牌取代ptype只是为了看看会发生什么并且有效。这是改变:

sales = sales.exclude(print__shirt__style__brand=brand)

如果我将exclude()更改为filter()并且不做任何其他更改,则它可以正常工作(除了显然与我需要的相反)。

sales = sales.filter(print__shirt__style__ptype=ptype)

为了完整起见,以下内容也会出现同样的错误

sales = sales.filter(~Q(print__shirt__style__ptype=ptype))

这是追溯:

Traceback:
File "/vhosts/domain.com/.virtualenvs/projectname/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/vhosts/domain.com/django/projectname/appname/admin_views.py" in popularity_report
  1032.     for s in sales:
File "/vhosts/domain.com/.virtualenvs/projectname/lib/python2.7/site-packages/django/db/models/query.py" in _result_iter
  118.                 self._fill_cache()
File "/vhosts/domain.com/.virtualenvs/projectname/lib/python2.7/site-packages/django/db/models/query.py" in _fill_cache
  892.                     self._result_cache.append(self._iter.next())
File "/vhosts/domain.com/.virtualenvs/projectname/lib/python2.7/site-packages/django/db/models/query.py" in iterator
  291.         for row in compiler.results_iter():
File "/vhosts/domain.com/.virtualenvs/projectname/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in results_iter
  763.         for rows in self.execute_sql(MULTI):
File "/vhosts/domain.com/.virtualenvs/projectname/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
  818.         cursor.execute(sql, params)
File "/vhosts/domain.com/.virtualenvs/projectname/lib/python2.7/site-packages/django/db/backends/util.py" in execute
  40.             return self.cursor.execute(sql, params)
File "/vhosts/domain.com/.virtualenvs/projectname/lib/python2.7/site-packages/django/db/backends/mysql/base.py" in execute
  114.             return self.cursor.execute(query, args)
File "/vhosts/domain.com/.virtualenvs/projectname/lib/python2.7/site-packages/MySQLdb/cursors.py" in execute
  201.             self.errorhandler(self, exc, value)
File "/vhosts/domain.com/.virtualenvs/projectname/lib/python2.7/site-packages/MySQLdb/connections.py" in defaulterrorhandler
  36.     raise errorclass, errorvalue

Exception Type: DatabaseError at /popularity/
Exception Value: (1054, "Unknown column 'appname_prints.ptype_id' in 'where clause'")

最后是一些版本号:

Django 1.4.5
Mysql Python 1.2.4

知道我能做什么吗?

0 个答案:

没有答案