[Django] #35466: Create new objects method get_or_none() to avoid boilerplate try/except

10 views
Skip to first unread message

Django

unread,
May 17, 2024, 4:29:42 PM5/17/24
to django-...@googlegroups.com
#35466: Create new objects method get_or_none() to avoid boilerplate try/except
-------------------------------------+-------------------------------------
Reporter: | Owner: nobody
sebastian-muthwill |
Type: New | Status: new
feature |
Component: Database | Version: dev
layer (models, ORM) | Keywords: models, query,
Severity: Normal | queryset, get
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
**Problem statement**
Currently one can retrieve a single object, besides other ways, via
`Model.objects.filter().first()`or `Model.objects.get()`

`Model.objects.get()` however will raise a `DoesNotExist` exception if
there is no result. This leads into the situation that one has to
try/except the query to handle it properly.

In some cases it is only relevant to do something if an entry exist.

**Proposed solution**
Provide a new method `django.db.models.query._BaseQuerySet.get_or_none()`
that returns an object if available and `None` if not.
--
Ticket URL: <https://code.djangoproject.com/ticket/35466>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
May 17, 2024, 5:20:39 PM5/17/24
to django-...@googlegroups.com
#35466: Create new objects method get_or_none() to avoid boilerplate try/except
-------------------------------------+-------------------------------------
Reporter: Sebastian Muthwill | Owner: nobody
Type: New feature | Status: closed
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution: wontfix
Keywords: models, query, | Triage Stage:
queryset, get | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Simon Charette):

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

Comment:

Please take a minute to
[https://code.djangoproject.com/query?description=~get_or_none&status=assigned&status=closed&status=new&order=id&desc=1
search the bug tracker] before requesting features
[https://docs.djangoproject.com/en/dev/internals/contributing/bugs-and-
features/#requesting-features as documented] and referenced in the
[https://code.djangoproject.com/newticket please read first section of the
ticket creation page]

Duplicate of #33384, #11352, #2659, all of which were wontfixed.
--
Ticket URL: <https://code.djangoproject.com/ticket/35466#comment:1>
Reply all
Reply to author
Forward
0 new messages