# Proposal: Add DoesNotExist and MultipleObjectsReturned to related managers

**URL:** <https://forum.djangoproject.com/t/proposal-add-doesnotexist-and-multipleobjectsreturned-to-related-managers/36701>\
**Category:** ORM\
**Created:** [November 23, 2024, 10:36pm UTC](https://forum.djangoproject.com/t/proposal-add-doesnotexist-and-multipleobjectsreturned-to-related-managers/36701 "2024-11-23T22:36:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nanorepublica](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/nanorepublica/32/14211_2.png) [@nanorepublica](https://forum.djangoproject.com/u/nanorepublica)\
**Post date:** [November 23, 2024, 10:36pm UTC](https://forum.djangoproject.com/t/proposal-add-doesnotexist-and-multipleobjectsreturned-to-related-managers/36701/1 "2024-11-23T22:36:33Z")

</div>

This is would be a nice usability gain as I have tried to do this a few times in the past and have been slightly annoyed when it didn’t work.

Essentially I want to allow this to work (and similarly for MultipleObjectsReturned):

```py
obj = MyModel.objects.get(pk=1)
try:
    obj.reverse_related_field.get(**kwargs)
except obj.reverse_related_field.DoesNotExist:
    # deal with the exception

```

The key difference here is not having to import a generic exception message.

From a bit of experimentation it seems like the code change is fairly simple (4 lines), plus tests and documentation.

The original short blog post here: [Django proposal: Adding DoesNotExist and MultipleObjectsReturned exceptions to the Related Manager - Software Crafts](https://softwarecrafts.uk/100-words/day-228)

---

<div class="post-metadata">

**Author:** ![Lily-Foote](https://avatars.discourse-cdn.com/v4/letter/l/c37758/32.png) [@Lily-Foote](https://forum.djangoproject.com/u/Lily-Foote)\
**Post date:** [November 23, 2024, 11:16pm UTC](https://forum.djangoproject.com/t/proposal-add-doesnotexist-and-multipleobjectsreturned-to-related-managers/36701/2 "2024-11-23T23:16:47Z")

</div>

Makes sense to me! I’m in favour of adding a ticket to solve this.

---

<div class="post-metadata">

**Author:** ![charettes](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/charettes/32/27_2.png) [@charettes](https://forum.djangoproject.com/u/charettes)\
**Post date:** [November 25, 2024, 5:33am UTC](https://forum.djangoproject.com/t/proposal-add-doesnotexist-and-multipleobjectsreturned-to-related-managers/36701/3 "2024-11-25T05:33:31Z")

</div>

Note that this can only work for many-reverse related descriptors as one-to-one reverse descriptor access immediately triggers a query

e.g.

```python
location = Location.objects.get(pk=1)
try:
    location.restaurant
except location.restaurant.DoesNotExist:
   # ^^ cannot be used as location.restaurant access will trigger
   # another `Restaurant.DoesNotExist` and possibly lead to
   # hard to diagnose issues?

```

Due to this asymmetry I’m not convinced this is worth adding.

---

<div class="post-metadata">

**Author:** ![shangxiao](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/shangxiao/32/10313_2.png) [@shangxiao](https://forum.djangoproject.com/u/shangxiao)\
**Post date:** [November 25, 2024, 7:19am UTC](https://forum.djangoproject.com/t/proposal-add-doesnotexist-and-multipleobjectsreturned-to-related-managers/36701/4 "2024-11-25T07:19:56Z")

</div>

This will work 🤔 (obtained from `get_object_or_404()`)

```python
try:
    obj.reverse_related_field.get(**kwargs)
except obj.reverse_related_field.model.DoesNotExist:
    # deal with the exception

```

---

<div class="post-metadata">

**Author:** ![nanorepublica](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/nanorepublica/32/14211_2.png) [@nanorepublica](https://forum.djangoproject.com/u/nanorepublica)\
**Post date:** [November 25, 2024, 8:58am UTC](https://forum.djangoproject.com/t/proposal-add-doesnotexist-and-multipleobjectsreturned-to-related-managers/36701/5 "2024-11-25T08:58:47Z")

</div>

The mention of asymmetry is interesting as from my perspective when using Django I feel that there is an asymmetry without this proposal 😅.

Essentially when I am dealing with a manager/queryset object I am expecting the API to be the same in terms of methods and attributes I can access, therefore I would expect to be able to access `DoesNotExist` and `MultipleObjectsReturned` on the ManyRelated managers and wouldn’t expect to find them on a OneToOne relationship or the One side of a ForeignKey.

@shangxiao thanks for the code snippet! I will be using that right now 😄
