On Dec 2, 2007 11:50 PM, James Bennett <[EMAIL PROTECTED]> wrote: > I guess I'm curious as to what's difficult or error-prone about it; > I've never run into a problem where slicing was the cuplrit, and it > feels like a more intuitive and Pythonic syntax than the "limit" and > "offset" arguments we used to have in the pre-magic-removal days.
Here's a specific problem that's bitten me a number of times. I want to get a list of model objects and do something special with the first one. I want to pass both the full list and the first one to the template. objects = MyModel.objects.filter(site=1) first_one = objects[0] do_something_special(first_one) return render_to_response('t.html', {'objects': objects, 'first': first_one}) Although this works, it performs two database queries -- one to retrieve all of the objects, and one to retrieve the first one (with a "LIMIT" clause). That's unacceptable. The (unintuitive) way to fix the problem is to wrap the first line in list() -- objects = list(MyModel.objects.filter(site=1)) The counter argument here is, "Well, that's exactly what you have to do with any iteratable." And my counter argument to that is, "Yeah, but normal iterables raise TypeError when you try to slice them." The confusion here stems from the fact that a QuerySet *sometimes* behaves like a list, but not always. After the QuerySet has been evaluated, then the slicing syntax simply selects items from the list, but before the QuerySet has been evaluated, the slicing syntax acts as a LIMIT/OFFSET. Different things happen depending on the context -- and that's what I find confusing. > Let's step back from the case at hand and consider something else so > it'll be a bit more objective to look at. That means another "magic" > Python method, so I'll pick the __str__()/__unicode__() combo we do on > model classes. Sorry for leading you astray and apparently causing you to spend a long time writing a blog entry about what is and isn't magic! I shouldn't have used the word "magic," as it's a loaded term and has different meanings in different contexts (much like QuerySet!). I'll restate my philosophical problem with this API without using that word. :-) My problem with the slicing syntax is that it's the *only* part of the QuerySet API that uses Python syntax hacking -- as in, "the plus sign now does something different," or "the slice operator now does something different." Everything else in the API uses normal Python methods. It's this inconsistency that bothers me. I'm still +1 on fixing this wart, but it won't be the end of the world if nobody agrees with me. I just want folks to understand my reasoning. Adrian -- Adrian Holovaty holovaty.com | djangoproject.com --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---