Opened 6 months ago

Closed 6 months ago

#35466 closed New feature (wontfix)

Create new objects method get_or_none() to avoid boilerplate try/except

Reported by: Sebastian Muthwill Owned by: nobody
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords: models, query, queryset, get
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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.

Change History (1)

comment:1 by Simon Charette, 6 months ago

Resolution: wontfix
Status: newclosed

Please take a minute to search the bug tracker before requesting features as documented and referenced in the please read first section of the ticket creation page

Duplicate of #33384, #11352, #2659, all of which were wontfixed.

Note: See TracTickets for help on using tickets.
Back to Top