Django

Code

Ticket #2217 (closed: fixed)

Opened 2 years ago

Last modified 2 years ago

Filtering by related object does not work

Reported by: anonymous Assigned to: russellm
Milestone: Component: Database layer (models, ORM)
Version: SVN Keywords:
Cc: Triage Stage: Unreviewed
Has patch: 0 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

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.

Attachments

Change History

06/23/06 07:23:25 changed by anonymous

  • component changed from Admin interface to Database wrapper.

06/27/06 05:45:31 changed 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.

06/30/06 20:14:41 changed by russellm

  • status changed from new to closed.
  • resolution set to fixed.

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

07/08/06 22:51:37 changed by russellm

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

07/08/06 22:53:12 changed 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.


Add/Change #2217 (Filtering by related object does not work)




Change Properties
Action