#2217 - Filtering using objects rather than IDs

2 views
Skip to first unread message

Russell Keith-Magee

unread,
Jun 30, 2006, 8:04:45 AM6/30/06
to Django Developers

Hi all,

I'm in the process of working on a fix for ticket #2217. The fix involves a slight change to the interpretation of one type of query argument.  To illustrate the change, consider the following three (almost identical) queries:

Reporter.objects.filter(article__id__exact=1)
Reporter.objects.filter(article__pk=1)
Reporter.objects.filter(article=a.id)

Note that in the third case, you specify the id of the object, not the object itself, even though the first and second case already provide explicit mechanisms for querying the id field.

Under the change I have implemented for #2217, the third query would become:

Reporter.objects.filter(article=a)

i.e., you provide the object itself to the query, rather than the ID. Behind the scenes, this is interpreted as a primary key comparison (so the result is effectively the same). The same behaviour can follow through to __in queries;

Reporter.objects.filter(article__in=[a1,a2])

The change makes sense to me, and the unit tests provide almost no resistance to the change. The only tests that rely upon the existing behaviour are in the m2m_and_m2o suite, and they don't rely specifically upon the existing behaviour.

However, since this change could effect people in the field, I thought I'd check for objections before I commit.

Comments?

Russ Magee %-)





Jay Parlar

unread,
Jun 30, 2006, 8:22:32 AM6/30/06
to django-d...@googlegroups.com
On 6/30/06, Russell Keith-Magee <freakb...@gmail.com> wrote:

>
> However, since this change could effect people in the field, I thought I'd
> check for objections before I commit.
>
> Comments?


I think that personally it will break one or two things for me, but
it's a breakage I'm glad to deal with.

I'm consistently surprised when I try something like
Reporter.objects.filter(article=a) and it doesn't work. It seems so
much like it *should* work that I keep forgetting.

Jay P.

Jacob Kaplan-Moss

unread,
Jun 30, 2006, 8:49:20 AM6/30/06
to django-d...@googlegroups.com
On Jun 30, 2006, at 7:04 AM, Russell Keith-Magee wrote:
> However, since this change could effect people in the field, I
> thought I'd check for objections before I commit.

I'm +1 as long as looking up by ID still works as well. In my mind,
both of these should do exactly the same thing::

Article.objects.filter(author=a1)
Article.objects.filter(author__pk=a1.id)

Jacob

Russell Keith-Magee

unread,
Jun 30, 2006, 9:51:30 AM6/30/06
to django-d...@googlegroups.com

Yes  - this will be the new behaviour. author_pk internally maps to author__id__exact; author maps to author__exact.

Russ Magee %-)

Tyson Tate

unread,
Jun 30, 2006, 11:59:19 AM6/30/06
to django-d...@googlegroups.com
On Jun 30, 2006, at 5:04 AM, Russell Keith-Magee wrote:

> ...
> Reporter.objects.filter(article=a)
>
> ...


> However, since this change could effect people in the field, I
> thought I'd check for objections before I commit.
>
> Comments?
>
> Russ Magee %-)

I'm +1 on this too. I've done it many a time, expecting it to "just
work".

Thanks!
-Tyson

Russell Keith-Magee

unread,
Jun 30, 2006, 9:20:20 PM6/30/06
to Django Developers

Ok - consensus seems to be its a good idea. However, it turns out I was a little premature in my warning. It turns out that in order to catch all the edge cases of OneToOne fields, i've implemented a fix that allows both kinds of object references. So, under the new scheme, if a.id = 1:
 
Reporter.objects.filter(article__id__exact=1)
Reporter.objects.filter(article__pk=1)
Reporter.objects.filter(article=1)
Reporter.objects.filter(article=a)

are all valid, equivalent searches. Similarly, for __in queries:

Reporter.objects.filter(article__in=[a1,a2])
Reporter.objects.filter(article__in=[1,a2])
Reporter.objects.filter(article__in=[1,2])

Are all valid (notice that the second query mixes integer form and object form references).

I've just committed r3246 which implements the change.

Russ Magee %-)

Gary Wilson

unread,
Jul 8, 2006, 12:08:11 AM7/8/06
to Django developers
Russell Keith-Magee wrote:
> I've just committed r3246 which implements the change.

Thanks Russell. This is certainly more natural.
This really should be added to the model api documentation too.

Gary Wilson

unread,
Jul 8, 2006, 10:43:41 AM7/8/06
to Django developers
Gary Wilson wrote:
> This really should be added to the model api documentation too.

db api rather.

bradford

unread,
Jul 9, 2006, 12:41:12 AM7/9/06
to Django developers
I never understood why you filter an object with foo=foo.id and you
create an object with foo=foo. Consistency is nice!

Russell Keith-Magee

unread,
Jul 9, 2006, 3:05:18 AM7/9/06
to django-d...@googlegroups.com

I've just committed some db-api changes (r3302). Let me know if you think they need any clarification.

Russ Magee %-) 
Reply all
Reply to author
Forward
0 new messages