Opened 9 years ago

Closed 9 years ago

Last modified 8 years ago

#2217 closed enhancement (fixed)

Filtering by related object does not work

Reported by: anonymous Owned by: russellm
Component: Database layer (models, ORM) Version: master
Severity: normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

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 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.

Change History (5)

comment:1 Changed 9 years ago by anonymous

  • Component changed from Admin interface to Database wrapper

comment:2 Changed 9 years ago by anonymous

  • Owner changed from adrian to russellm
  • Version set to SVN

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.

comment:3 Changed 9 years ago by russellm

  • Resolution set to fixed
  • Status changed from new to closed

(In [3246]) Fixed #2217 -- Allowed raw objects to be used in exact and in query terms. Existing use of primary keys in query terms is preserved.

comment:4 Changed 9 years ago by russellm

(In [3301]) Refs #2217 -- Updated DB API docs to discuss filtering using objects rather than IDs.

comment:5 Changed 9 years ago by russellm

(In [3302]) Refs #2217 -- Updated DB API docs to discuss filtering using objects rather than IDs. Second attempt - forgot to save before commit last time.

Note: See TracTickets for help on using tickets.
Back to Top