Doesn’t it work to do qs[0] ?

On Sat, 13 Apr 2019 at 17:48, alTus <mortas...@gmail.com> wrote:

> Hello.
>
> Please consider if that proposal can be useful not only for me :)
>
> `QuerySet.first` is quite useful shortcut but its drawback is that it
> involves ordering so some DB queries become expensive.
> But quite often (and very often while debugging) there's a need just to
> get one object that satisfy some filters.
>
> Current `first` implementation to compare with:
>
>     def first(self):
>         """Return the first object of a query or None if no match is
> found."""
>         for obj in (self if self.ordered else self.order_by('pk'))[:1]:
>             return obj
>
> Proposal (as an example of implementation):
>
>     def one(self):
>         """Return one random object of a query or None if no match is
> found."""
>         for obj in self.order_by()[:1]:
>             return obj
>
> That would be short, simple and wouldn't require any imports in client
> code.
>
> Thank you.
>
>
> --
> 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+unsubscr...@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/c4337df6-c0f3-44fe-bbc0-01fe01cdf621%40googlegroups.com
> <https://groups.google.com/d/msgid/django-developers/c4337df6-c0f3-44fe-bbc0-01fe01cdf621%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>
-- 
Adam

-- 
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+unsubscr...@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/CAMyDDM0L7ySa7DYnh_to7oNKFhasK1iqW7zZY_c9JRs6d-qfGQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to