Add a first method to the QuerySet
|Reported by:||Wim Feijen||Owned by:||nobody|
|Component:||Database layer (models, ORM)||Version:||master|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
Many times, get() is being used inside a try except Model.DoesNotExist clause. In order to save us lines of code and in order to prevent the Exception becoming common, a discussion followed which can be found here:
According to our BDFL:
After thinking a bit, I'm +1 on the idea, and I think
Queryset.first() is the right name (which implies we should probably
last() for completion).
This mirrors the name used by a few other ORM/collection APIs that I
think have really nice APIS:
- SQLalchemy (http://docs.sqlalchemy.org/en/rel_0_7/orm/query.html#sqlalchemy.orm.query.Query.first)
- Storm (http://people.canonical.com/~therve/storm/storm.store.ResultSet.html#first)
- Backbone (http://backbonejs.org/#Collection-Underscore-Methods /
If someone produces a patch I'll review it and commit it.