Hi all,
On Thu, 11 Oct 2012, Daniel Moisset wrote:
> obj, = SomeModel.objects.filter(foo='bar') or [None]
Daniel's solution is elegant, but far from clear or clean.
I'm strongly in favour of a simple, obvious way to do the common thing,
which is to return None if the object doesn't exist, instead of throwing
an exception. My preferred method names would be .nget(), .get_or_none()
or .first().
May I invoke the Zen of Python to add more than +1 to votes for this
change? (not that my vote counts anyway, of course :)
+1 Beautiful is better than ugly.
+1 Explicit is better than implicit.
+1 Simple is better than complex.
+1 Flat is better than nested.
+1 Readability counts.
-1 Special cases aren't special enough to break the rules.
+1 Although practicality beats purity.
+1 There should be one-- and preferably only one --obvious way to do
it.
+1 Now is better than never. :)
+1 If the implementation is hard to explain, it's a bad idea. (the
alternative one-liner proposed by Daniel is hard to explain)
+1 If the implementation is easy to explain, it may be a good idea.
+9 Total
Cheers, Chris.
--
Aptivate |
http://www.aptivate.org | Phone:
+44 1223 967 838
Future Business, Cam City FC, Milton Rd, Cambridge, CB4 1UY, UK
Aptivate is a not-for-profit company registered in England and Wales
with company number 04980791.