Feature request: get_first_or_404

128 views
Skip to first unread message

Piotr Domański

unread,
Nov 28, 2017, 5:41:54 PM11/28/17
to Django developers (Contributions to Django itself)
Currently we have get_object_or_404 and get_list_or_404 but sometimes we just want to get first matching record for some criteria.
If criteria aren't unique then get_object_or_404 raises MultipleObjectsReturned so request ends with internal server error.

My idea is to add function which returns just first matching element and ends with 404 if no elements match.
This function will have parameter order_by (optional or required) of type List['str'] 
which determines which element choose first if there are many matching elements.

In my opinion addition of this feature is good idea, because it is useful and safe for Django framework.

Tim Graham

unread,
Nov 28, 2017, 6:33:24 PM11/28/17
to Django developers (Contributions to Django itself)
A quick search reveals a gist that contains a number of similar helpers.
https://gist.github.com/stkrp/47c536b681082c638849e9a89f77ff05

I think the main problem of going down this path is deciding which are suitable to add and which aren't. I'm not sure that any of them meet the "80% of projects might use this" criteria, and they aren't difficult to implement outside of Django itself.

Josh Smeaton

unread,
Nov 28, 2017, 8:31:24 PM11/28/17
to Django developers (Contributions to Django itself)
From my experiences, all projects I've worked on have implemented get_first_or_none and get_first_or_404, and usually slightly differently. I think these shortcuts would make sense for Django. Interested to hear how common the usage would be from others on the list though.

Collin Anderson

unread,
Nov 28, 2017, 10:08:52 PM11/28/17
to django-d...@googlegroups.com
Is there a different between get_first_or_none() and the already existing first()?

If we do first, some may want get_last_*() for everything too :)

On Tue, Nov 28, 2017 at 8:31 PM, Josh Smeaton <josh.s...@gmail.com> wrote:
From my experiences, all projects I've worked on have implemented get_first_or_none and get_first_or_404, and usually slightly differently. I think these shortcuts would make sense for Django. Interested to hear how common the usage would be from others on the list though.

--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-developers+unsubscribe@googlegroups.com.
To post to this group, send email to django-developers@googlegroups.com.
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/fcabccc7-3da2-456f-ad92-90145b700635%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Sergey Fedoseev

unread,
Nov 29, 2017, 2:20:39 AM11/29/17
to Django developers (Contributions to Django itself)
IMO it shouldn't be added to Django because the same thing can be done in one line: get_object_or_404(qs[:1]), but we could add such using to get_object_or_404() docs.

Robert Roskam

unread,
Nov 30, 2017, 8:15:38 AM11/30/17
to Django developers (Contributions to Django itself)
I'd like a better fleshed out example from OP, as it seems trivial to implement yourself whenever you need it. It may be that I'm not understanding the suggestion.
Reply all
Reply to author
Forward
0 new messages