W dniu 06.03.2009 20:06, Jeff Gentry pisze:
> On Fri, 6 Mar 2009, Malcolm Tredinnick wrote:
>
>>> Bob.objects.filter(foo=myFoo).filter(blah__in=myBlahs)
>>>
>> Seems like the best (and obvious) way to me.
>>
>
> Gotcha.
>
>
>> Yes it does. As written, your models have no ordering requirements. That
>> complete lack of constraint is preserved perfectly. :-)
>>
>
> True ;)
>
>
>> Precisely. If you want ordering, you have to specify it. The database is
>> in no way obliged to return rows even in the order they are stored on
>> disk, so if you're relying on that, you are making an error.
>>
>
> Fair enough, that's what I expected as I know that SQL IN isn't guaranteed
> to preserve anything. I thought that there might be some chance that
> Django was doing something on top of that, but doubted it (and turned out
> to be right).
>
> Here's a followup though. To go back to my original model descriptions:
>
> class Foo:
> asdf = models.CharField()
>
> class Blah:
> qwerty = models.CharField()
>
> class Bob:
> foo = models.ForeignKey(Foo)
> blah = models.ForeignKey(Blah)
>
> Suppose that for a given Foo (say foo.id = 42), I want the Bobs associated
> with the entire set of Blahs. In SQL I could do something like this
> (might not be exactly right, but should demonstrate the idea):
>
> select * from app_bob,app_blah where app_bob.blah_id=app_blah.id and
> app_foo_id=42;
>
> I'd imagine this would be faster than going through the IN directive
> (particularly considering that the entire set of Blahs will typically be
> 50k-250k in length). I know that I can drop down to writing raw SQL
> queries but was trying to see if there was a way I could do something like
> this (if this is actually a smart query to do at all) via the ORM. I'm
> currently looking at the extra() command but can't seem to get the right
> mojo to get that working.
>
> What's the right way to get this one working?
>
> Thanks
> -J
>
this supposed to work:
blah_subquery="blah_id IN (%s)" %
Blah.objects.values("id").filter(qwerty__icontains='somethink').query
Bob.objects.filter(foo=myFoo).extra(where=blah_subquery)
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---