Luke Plant wrote:
> On Tuesday 04 December 2007 07:25:31 Gary Wilson wrote:
> 
>> Sort of my point.  Since filter() and [:] both return QuerySets why
>> should they be any different:
>>
>> UTPerson.objects.all().filter(name__startswith='a')[:10]
> 
> 1) This, logically, should return the first ten people whose 'name' 
> starts with 'a'.
> 
>> UTPerson.objects.all()[:10].filter(name__startswith='a')
> 
> 2) This should return the people whose 'name' starts with 'a' from the 
> first 10 rows in the table
> 
> This is almost certainly what most people would expect (I guess we could 
> argue about that...)  But there is no way to specify it in SQL without 
> doing subselects -- you will end up doing 1) instead.  

Does that mean that most people also expect:

UTPerson.objects.filter(first_name__startswith='a').filter(last_name__startswith='b')

to return the people whose last name starts with 'b' from the people whose
first name starts with 'a'?  Maybe, but filter doesn't work that way just like
slicing wouldn't work that way.

I should be able to keep piling things onto the QuerySet.  You can do it with
all the other QuerySet methods that return QuerySets.  Why not with the
slicing syntax (which returns a QuerySet object)?

I am just trying to spark some discussion on the matter.  Personally, I
wouldn't mind if slices were changed to always return lists (like the do
currently if you use a step).  And then we and in a limit() method for people
who want QuerySets returned so that they can be built up and lazily evaluated.

I see:

UTPerson.objects.all().limit(10).filter(...).limit(10)

acting like order_by() in that the second limit would override the first.

> Remember that there are other methods that do not or cannot commute e.g. 
> trying to do order_by() after slicing -- it would change the objects 
> that are returned.  Just because some methods do happen to commute 
> (filtering) doesn't mean that others should.

Well, with order_by() you can do:

UTPerson.objects.all().order_by('first_name').filter(...).order_by('last_name')

In this case, the second order_by() overrides the first.

Gary

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to django-developers@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to