#2217 closed enhancement (fixed)
Filtering by related object does not work
Reported by: | anonymous | Owned by: | Russell Keith-Magee |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
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 Book
s 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.
Change History (5)
comment:1 by , 19 years ago
Component: | Admin interface → Database wrapper |
---|
comment:2 by , 19 years ago
Owner: | changed from | to
---|---|
Version: | → SVN |
comment:3 by , 18 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
comment:4 by , 18 years ago
comment:5 by , 18 years ago
Note:
See TracTickets
for help on using tickets.
yes - the fix is relatively minor; RelatedField needs a get_db_prep_lookup method.
I have a patch, but it has revealed a few other problems with field lookup. I'll wait until I have those resolved before I commit the fix.