On Tue, Oct 9, 2012 at 7:29 AM, Anssi Kääriäinen <anssi.kaariai...@thl.fi>wrote:
> On 9 loka, 17:05, Ole Laursen <o...@iola.dk> wrote: > > Hi! > > > > What do people think of > > > > A.objects.getdefault(slug="hello") # returns None if slug doesn't > exist > > A.objects.getdefault(slug="hello", default=A()) # returns empty object > > if slug doesn't exist > > > > I find that in practice, most of the time it would be better to get None > > back instead of the DoesNotExist exception, but changing .get() is of > > course impossible without breaking the API. > > > > I do think that > > > > A.objects.get(slug="hello", default=None) > > > > is more elegant, but it might break some code. Although it does seem > really > > unlikely anyone would add a field named "default" to their model and then > > use it to locate a unique object. > > > > The only related info I found in the issue tracker and on this list was a > > thread from 2006. > > My approach would be to define .nget() method - just a shorthand for: > try: > obj = qs.get(somecondition) > except DoesNotExist: > obj = None > > This would be really simple to implement, and I know I would use it. > It would also be simple to document: > "works exacltly like .get(), but when .get() raises > DoesNotExist .nget() returns None". > > If the .getdefault() is wanted, then we could always > have .get(somecondition, __default=None). Using '__' as prefix will > guarantee there will be no name clashes. > > - Anssi > Hi - Personally I would prefer to override the get method intentionally on a per model basis. Or add a customized nget type method as is suggested by Ansii. I could see new coders getting in the habit of using something like getdefault without putting enough thought into it. As an engineer I tend to think that beauty is achieved not by what you add but by what you take away so I would personally vote to keep this out of Django. But I wouldn't be distraught if it made it in. -CJ- > -- > 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 > django-developers+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/django-developers?hl=en. > > -- 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 django-developers+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-developers?hl=en.