#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. -- 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 django-updates+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/0107018f883ef1aa-9c798627-fbf7-4e75-9bc0-6c24223de1c3-000000%40eu-central-1.amazonses.com.