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 -~----------~----~----~----~------~----~------~--~---

