{{{#!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>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* 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>
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>
* status: new => closed
* resolution: => duplicate
Comment:
Duplicate of #2659, #11352.
--
Ticket URL: <https://code.djangoproject.com/ticket/27242#comment:3>
Comment (by arogachev):
Sorry. I tried to use search to find if similar issues are already exist
(I was pretty sure about that), but din't find them.
Now it's clear to me - it needs to be included in own codebase.
--
Ticket URL: <https://code.djangoproject.com/ticket/27242#comment:4>