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 [email protected].
To post to this group, send email to [email protected].
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.
For more options, visit https://groups.google.com/d/optout.

Reply via email to