Filtering by the related object does not work and results in a errorFor example, for the following models:
class Author(models.Model):
name = models.CharField(maxlength=100)
class Book(models.Model):
title = models.CharField(maxlength=100)
author = models.ForeignKey(Author)
filtering all Books with certain author: Book.objects.filter(author=an_author_object) results in the following error message:
Traceback (most recent call last):
File "<console>", line 1, in ?
File "C:\Python24\lib\site-packages\django\db\models\query.py", line 88, in __
repr__
return repr(self._get_data())
File "C:\Python24\lib\site-packages\django\db\models\query.py", line 412, in _
get_data
self._result_cache = list(self.iterator())
File "C:\Python24\lib\site-packages\django\db\models\query.py", line 163, in i
terator
cursor.execute("SELECT " + (self._distinct and "DISTINCT " or "") + ",".join
(select) + sql, params)
File "C:\Python24\lib\site-packages\django\db\backends\util.py", line 12, in e
xecute
return self.cursor.execute(sql, params)
ProgrammingError: ERROR: syntax error at or near "object" at character 121
SELECT "app_book"."id","app_book"."title","app_book"."author_id" FROM "app_book"
WHERE ("app_book"."author_id" = Author object)
However, filtering using the related object's primary key works as excepted: Book.objects.filter(an_author_object.id) results in correct list of results to be returned.
Changing the behaviour is probably quite trivial and would result in a more natural way of filtering by related objects.