I think it's a good idea, but the implementation needs some work.
As a default it should keep working as it does now, to keep backwards
compatibility.
So I think David's idea is a good one, but I think the method name
should be more descriptive, like get_or_none and latest_or_none
On Mar 8, 5:02
I definitely would like to see this handled in Django, but not in the
way mentioned. I personally think there does not need to be an option
for what it raises. I think of this in the situations where find
methods return either a -1, or an Exception, based on which method you
call.
Now I can't come
http://code.djangoproject.com/ticket/5741
On Sun, Mar 7, 2010 at 11:48 PM, Chris wrote:
> In my projects, I use this pattern a lot:
>
> try:
> obj = SomeModel.objects.filter(**crazy_filters_here).latest()
> except SomeModel.DoesNotExist:
> obj = None
>
> Basically, I throw a filter at a que
> PS: Can people *please* use 'cls' and not 'klass'. =/
Yes you are right, my fault. Although I did just rip the code from
the patch at http://code.djangoproject.com/ticket/2659 and modify it.
Additionally, looking at that issue will likely give some insight into
the response this change will li
A couple of things:
* Technically, a queryset doesn't raise DoesNotExist; a model does.
When you call latest(), you call the latest model in the queryset,
which, in the case of an empty qs, raises DoesNotExist
* (bite me) I work with lossy data and very often have to deal with
broken relations. I'
Seems like it'd be pretty simple for the developer to make a little
helper method like so...
>>> def latest_or_none(klass, *args, **kwargs):
>>> try:
>>> return klass.objects.filter(*args, **kwargs).latest()
>>> except klass.DoesNotExist:
>>> return None
I guess I don't se
I'm not sure what reception you'll get for this suggestion (also given
that all attention is currently on Django 1.2), but I think eventually
problems like these may be best addressed by allowing you as the
developer to write your own customised helper functions/methods to
deal with things the way
In my projects, I use this pattern a lot:
try:
obj = SomeModel.objects.filter(**crazy_filters_here).latest()
except SomeModel.DoesNotExist:
obj = None
Basically, I throw a filter at a queryset and if some value comes out,
I use it, If the filter happens to filter out all rows, I want the