#27242: Add get_object_or_none to django.shortcuts
-------------------------------------+-------------------------------------
     Reporter:  arogachev            |                    Owner:  nobody
         Type:  New feature          |                   Status:  new
    Component:  Core (Other)         |                  Version:  master
     Severity:  Normal               |               Resolution:
     Keywords:  object, model,       |             Triage Stage:
  shortcut                           |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Description changed by arogachev:

Old description:

> Quite often we need to find object of certain model and return `None` if
> it does not exist. I suggest to add this to `django.shortcuts`:
>
> {{{#!python
> def get_object_or_none(klass, *args, **kwargs):
>     queryset = _get_queryset(klass)
>     try:
>         return queryset.get(*args, **kwargs)
>     except queryset.model.DoesNotExist:
>         return None
> }}}
>
> Yes, we can easily create this in our own code, but maybe consider to add
> this to the core?

New description:

 Quite often we need to find object of certain model and return `None` if
 it does not exist. I suggest to add this to `django.shortcuts` similar to
 `get_object_or_404`:

 {{{#!python
 def get_object_or_none(klass, *args, **kwargs):
     queryset = _get_queryset(klass)
     try:
         return queryset.get(*args, **kwargs)
     except queryset.model.DoesNotExist:
         return None
 }}}

 Yes, we can easily create this in our own code, but maybe consider to add
 this to the core?

--

--
Ticket URL: <https://code.djangoproject.com/ticket/27242#comment:2>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/067.9666337accf14c1dbb1a13346e60b509%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to