Opened 19 years ago

Closed 19 years ago

Last modified 18 years ago

#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 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 by anonymous, 19 years ago

Component: Admin interfaceDatabase wrapper

comment:2 by anonymous, 19 years ago

Owner: changed from Adrian Holovaty to Russell Keith-Magee
Version: 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 by Russell Keith-Magee, 19 years ago

Resolution: fixed
Status: newclosed

(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 by Russell Keith-Magee, 18 years ago

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

comment:5 by Russell Keith-Magee, 18 years ago

(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