On Apr 10, 8:30 am, Kevin Teague <ke...@bud.ca> wrote: > There is nothing exceptional about exceptions in Python. McDonc does a > good job at explaining how to think about exceptions as less than > exceptional: > > http://plope.com/Members/chrism/exceptions_arent_errors/view > > The only arguably exceptional part is that ideally the ORM would raise > a plain KeyError exception instead of a custom exception when a key > look-up fails ...
It makes a difference if you are likely to find an object or not - at least in terms of performance. If you are expecting to find an object, (and only one), the setup of a try: except: clause is minimal, but if you are not finding an object more often than not, it turns out to be much slower to have the exception raised. Having said that, the cost of having two DB queries may overcome that anyway: len(queryset) followed by queryset.get() is probably going to hit the database twice. A better solution would be to do a queryset.all(), see if the length is 1, and then get the first value by index 0. Matt. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.