Ah.. makes sense. I'd noticed that filter() returns QuerySet, but since QuerySet is essentially polymorphic as a list, I keep forgetting that's not what it is.. Obviously then, throwing an exception at filter () can't work, and throwing the exception upon iteration (even if possible) would be nasty.
At any rate. I promise I'm not as desperate as I sound. Obviously, it's a trivial issue. Many apologies for asking an apparently unpopular question. Phill On Mar 30, 4:03 pm, Malcolm Tredinnick <[email protected]> wrote: > On Mon, 2009-03-30 at 15:53 -0700, famousactress wrote: > > > Your permission is appreciated :) > > > That said, wrapping up functions for utility is kinda the point of an > > API so I figured I'd toss the question out to see whether or not > > something that does this already exists or if enough folks think it > > ought to to merit a patch. I'm curious what the motivation is for > > having get() throw an exception, while filter() returns an empty > > list... seems inconsistent. > > The functionality you so desperately seek if you want to go one way ikf > the objects exists and another way if it doesn't already exists and has > been revealed to you. It's called get(). Raising an exception when > something is missing *is* exceptional. If you're going to program in > Python, get used to catching exceptions and dealing with them now and > again. > > You're also labouring under an incorrect assumption here: filter() > doesn't return a list. This is documented. It returns another queryset. > Querysets are iterable and sometimes it is iterating over an empty > result set -- you don't know that, because Django doesn't either, until > you start iterating. The get() method on the other hand, does not return > a queryset, it returns an object, or raises an exception. They return > quite different objects (and despite your initial guess, it would be > highly inconsistent -- and pretty much impossible on an implementation > level -- for a queryset to return None when you iterate over it if it > contains no results). > > There's no reason for a patch to Django to implement what you're after > because it's already possible. Either use get() or convert the queryset > to a list and use "if list(qs):" to detect your paths. Both require only > one line of code to delimit each path of action, so it's hardly onerous. > > Regards, > Malcolm --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---

