If you just want one object that matches your filters during debugging, without 
overhead, why don’t you use .last() instead?

> On 14 Apr 2019, at 7:14 am, Tom Forbes <t...@tomforb.es> wrote:
> 
> I don’t think we should add a method like this for a few reasons. Firstly 
> without an order by in SQL the order of rows is undefined, in practice myql 
> orders rows but Postgres returns a different order per transaction. This will 
> be confusing to users who don’t understand this and come to implicitly rely 
> on first() being stable.
> 
> Secondly if you are filtering on an indexed column the overhead will be next 
> to none. Is this not the case?
> 
> And lastly, changing this would be major backwards incompatible change.
> 
> Tom
> 
>> On 13 Apr 2019, at 21:58, alTus <mortas...@gmail.com> wrote:
>> 
>> I've posted the source code if `first`. You can see there that if qs is not 
>> ordered then ordering by pk is added.
>> It's the main point of this issue btw.
>> 
>> суббота, 13 апреля 2019 г., 22:53:52 UTC+3 пользователь Florian Apolloner 
>> написал:
>>> 
>>> qs.order_by().first() should do what you want and is not worth adding .one()
>>> 
>>> Cheers,
>>> Florian
>>> 
>>>> On Saturday, April 13, 2019 at 5:48:29 PM UTC+2, alTus 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/fc6e71e5-a9b5-4cb5-9283-de1130ff0b2e%40googlegroups.com.
>> For more options, visit https://groups.google.com/d/optout.
> 
> -- 
> 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/B83882F9-E932-4258-93D7-100A3D4A933D%40tomforb.es.
> For more options, visit https://groups.google.com/d/optout.

-- 
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/DCA537BC-A189-4D59-9C65-10C4C3732B27%40gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to