33Following methods allow you to load data from the database.
44
55* ` get(*args, **kwargs) -> Model `
6+ * ` get_or_none(*args, **kwargs) -> Optional[Model] `
67* ` get_or_create(_defaults: Optional[dict[str, Any]] = None, *args, **kwargs) -> tuple[Model, bool] `
78* ` first(*args, **kwargs) -> Model `
9+ * ` first_or_none(*args, **kwargs) -> Optional[Model] `
10+ * ` last(*args, **kwargs) -> Model `
11+ * ` last_or_none(*args, **kwargs) -> Optional[Model] `
812* ` all(*args, **kwargs) -> list[Optional[Model]] `
913* ` iterate(*args, **kwargs) -> AsyncGenerator[Model] `
1014
@@ -15,8 +19,12 @@ Following methods allow you to load data from the database.
1519
1620* ` QuerysetProxy `
1721 * ` QuerysetProxy.get(*args, **kwargs) ` method
22+ * ` QuerysetProxy.get_or_none(*args, **kwargs) ` method
1823 * ` QuerysetProxy.get_or_create(_defaults: Optional[dict[str, Any]] = None, *args, **kwargs) ` method
1924 * ` QuerysetProxy.first(*args, **kwargs) ` method
25+ * ` QuerysetProxy.first_or_none(*args, **kwargs) ` method
26+ * ` QuerysetProxy.last(*args, **kwargs) ` method
27+ * ` QuerysetProxy.last_or_none(*args, **kwargs) ` method
2028 * ` QuerysetProxy.all(*args, **kwargs) ` method
2129
2230## get
@@ -126,6 +134,58 @@ album = await Album.objects.first()
126134assert album.name == ' The Cat'
127135```
128136
137+ ## first_or_none
138+
139+ ` first_or_none(*args, **kwargs) -> Optional[Model] `
140+
141+ Exact equivalent of ` first ` described above but instead of raising ` NoMatch `
142+ returns ` None ` if no db record matching the criteria is found.
143+
144+ ``` python
145+ empty = await Album.objects.first_or_none()
146+ # None — no rows in the table
147+ missing = await Album.objects.first_or_none(name = ' The Missing' )
148+ # None — no row matches the filter
149+ ```
150+
151+ ## last
152+
153+ ` last(*args, **kwargs) -> Model `
154+
155+ Gets the last row from the db ordered by primary key column descending.
156+ Complementary to ` first() ` — the default pk ordering is flipped and the top
157+ row is returned. When you combine ` last() ` with ` order_by(...) ` , the user's
158+ ordering is flipped too, so ` order_by("name").last() ` returns the row that
159+ would sort last by name.
160+
161+ ``` python
162+ await Album.objects.create(name = ' The Cat' )
163+ await Album.objects.create(name = ' The Dog' )
164+ album = await Album.objects.last()
165+ # last row by primary_key column desc
166+ assert album.name == ' The Dog'
167+
168+ album = await Album.objects.order_by(" name" ).last()
169+ # last row by name (alphabetical)
170+ assert album.name == ' The Dog'
171+ ```
172+
173+ !!!warning
174+ Same as ` first() ` — raises ` NoMatch ` if no rows and ` MultipleMatches ` if
175+ somehow more than one row matches.
176+
177+ ## last_or_none
178+
179+ ` last_or_none(*args, **kwargs) -> Optional[Model] `
180+
181+ Exact equivalent of ` last ` described above but instead of raising ` NoMatch `
182+ returns ` None ` if no db record matching the criteria is found.
183+
184+ ``` python
185+ empty = await Album.objects.last_or_none()
186+ # None — no rows in the table
187+ ```
188+
129189## all
130190
131191` all(*args, **kwargs) -> list[Optional["Model"]] `
@@ -252,6 +312,30 @@ related objects from other side of the relation.
252312!!!tip
253313 To read more about ` QuerysetProxy ` visit [ querysetproxy] [ querysetproxy ] section
254314
315+ ### first_or_none
316+
317+ Works exactly the same as [ first_or_none] ( ./#first_or_none ) function above but
318+ returns ` None ` instead of raising ` NoMatch ` , and works on the relation side.
319+
320+ !!!tip
321+ To read more about ` QuerysetProxy ` visit [ querysetproxy] [ querysetproxy ] section
322+
323+ ### last
324+
325+ Works exactly the same as [ last] ( ./#last ) function above but allows you to query
326+ related objects from other side of the relation.
327+
328+ !!!tip
329+ To read more about ` QuerysetProxy ` visit [ querysetproxy] [ querysetproxy ] section
330+
331+ ### last_or_none
332+
333+ Works exactly the same as [ last_or_none] ( ./#last_or_none ) function above but
334+ returns ` None ` instead of raising ` NoMatch ` , and works on the relation side.
335+
336+ !!!tip
337+ To read more about ` QuerysetProxy ` visit [ querysetproxy] [ querysetproxy ] section
338+
255339### all
256340
257341Works exactly the same as [ all] ( ./#all ) function above but allows you to query related
0 commit comments