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