#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
-------------------------------------+-------------------------------------
Changes (by arogachev):

 * needs_better_patch:   => 0
 * needs_tests:   => 0
 * needs_docs:   => 0


Old description:

> I think 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`:

 {{{#!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:1>
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/067.d52c45020e3a1a81a4a9509fb416fbbd%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to