Gets a single row from the db matching the criteria set by args/kwargs. Passing args/kwargs is a shortcut for ``filter(*args, **kwargs).get()``. When criteria are set (either through args/kwargs here or via a chained :meth:`filter`/:meth:`exclude`) the query fetche
(self, *args: Any, **kwargs: Any)
| 1141 | return None |
| 1142 | |
| 1143 | async def get(self, *args: Any, **kwargs: Any) -> "T": # noqa: CCR001 |
| 1144 | """ |
| 1145 | Gets a single row from the db matching the criteria set by args/kwargs. |
| 1146 | |
| 1147 | Passing args/kwargs is a shortcut for ``filter(*args, **kwargs).get()``. |
| 1148 | |
| 1149 | When criteria are set (either through args/kwargs here or via a |
| 1150 | chained :meth:`filter`/:meth:`exclude`) the query fetches every |
| 1151 | matching row and asserts that exactly one comes back — ``NoMatch`` |
| 1152 | is raised when none match and ``MultipleMatches`` when more than one |
| 1153 | matches. |
| 1154 | |
| 1155 | When no criteria are set, ``get()`` falls back to returning the last |
| 1156 | row of the table ordered by primary key descending (``LIMIT 1``); in |
| 1157 | that mode ``MultipleMatches`` cannot be raised. |
| 1158 | |
| 1159 | :raises NoMatch: if no rows match the criteria |
| 1160 | :raises MultipleMatches: when criteria are set and more than one row |
| 1161 | matches them. |
| 1162 | :param kwargs: fields names and proper value types |
| 1163 | :type kwargs: Any |
| 1164 | :return: returned model |
| 1165 | :rtype: Model |
| 1166 | """ |
| 1167 | if kwargs or args: |
| 1168 | return await self.filter(*args, **kwargs).get() |
| 1169 | |
| 1170 | if not self.filter_clauses: |
| 1171 | expr = self.build_select_expression( |
| 1172 | limit=1, |
| 1173 | order_bys=( |
| 1174 | [ |
| 1175 | OrderAction( |
| 1176 | order_str=f"-{self.model.ormar_config.pkname}", |
| 1177 | model_cls=self.model_cls, # type: ignore |
| 1178 | ) |
| 1179 | ] |
| 1180 | if not any([x.is_source_model_order for x in self.order_bys]) |
| 1181 | else [] |
| 1182 | ) |
| 1183 | + self.order_bys, |
| 1184 | ) |
| 1185 | else: |
| 1186 | expr = self.build_select_expression() |
| 1187 | |
| 1188 | async with self.model_config.database.get_query_executor() as executor: |
| 1189 | rows = await executor.fetch_all(expr) |
| 1190 | processed_rows = await self._process_query_result_rows(rows) |
| 1191 | if self._prefetch_related and processed_rows: |
| 1192 | processed_rows = await self._prefetch_related_models(processed_rows, rows) |
| 1193 | self.check_single_result_rows_count(processed_rows) |
| 1194 | return processed_rows[0] # type: ignore |
| 1195 | |
| 1196 | async def get_or_create( |
| 1197 | self, |