Django“where子句中的未知列”,用于排除,但不用于筛选
我有一个销售模型,外键指向打印模型,外键指向衬衫模型,最后外键指向样式模型。在最后一个模型中,有一个ptype字段,它是另一个模型的外键 这些列都存在于mysql表中,它已经使用了一段时间,但我仍然进行了四次检查 这是我的代码:Django“where子句中的未知列”,用于排除,但不用于筛选,django,django-models,Django,Django Models,我有一个销售模型,外键指向打印模型,外键指向衬衫模型,最后外键指向样式模型。在最后一个模型中,有一个ptype字段,它是另一个模型的外键 这些列都存在于mysql表中,它已经使用了一段时间,但我仍然进行了四次检查 这是我的代码: sales = sales.exclude(print__shirt__style__ptype=ptype) 这给了我一个错误: (1054, "Unknown column 'appname_prints.ptype_id' in 'where clause'")
sales = sales.exclude(print__shirt__style__ptype=ptype)
这给了我一个错误:
(1054, "Unknown column 'appname_prints.ptype_id' in 'where clause'")
请注意,它在打印模型中查找ptype_id字段,而不是在样式模型中。为什么它忽略了“衬衫风格”
样式模型还有一个品牌外键。我试着用brand替换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
知道我能做什么吗