[Django] #27242: Add get_object_or_none to django.shortcuts

16 views
Skip to first unread message

Django

unread,
Sep 19, 2016, 3:53:08 AM9/19/16
to django-...@googlegroups.com
#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 | Keywords: object, model, shortcut
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
------------------------------+-------------------------------------
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?

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

Django

unread,
Sep 19, 2016, 3:54:29 AM9/19/16
to django-...@googlegroups.com
#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

unread,
Sep 19, 2016, 3:55:07 AM9/19/16
to django-...@googlegroups.com
#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

unread,
Sep 19, 2016, 5:17:09 AM9/19/16
to django-...@googlegroups.com
#27242: Add get_object_or_none to django.shortcuts
-------------------------------------+-------------------------------------
Reporter: arogachev | Owner: nobody
Type: New feature | Status: closed

Component: Core (Other) | Version: master
Severity: Normal | Resolution: duplicate

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 timgraham):

* status: new => closed
* resolution: => duplicate


Comment:

Duplicate of #2659, #11352.

--
Ticket URL: <https://code.djangoproject.com/ticket/27242#comment:3>

Django

unread,
Sep 19, 2016, 5:47:24 AM9/19/16
to django-...@googlegroups.com
#27242: Add get_object_or_none to django.shortcuts
-------------------------------------+-------------------------------------
Reporter: arogachev | Owner: nobody
Type: New feature | Status: closed

Component: Core (Other) | Version: master
Severity: Normal | Resolution: duplicate
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
-------------------------------------+-------------------------------------

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>

Reply all
Reply to author
Forward
0 new messages