Return the earliest object according to fields (if given) or by the model's Meta.get_latest_by.
(self, *fields)
| 1138 | return params |
| 1139 | |
| 1140 | def _earliest(self, *fields): |
| 1141 | """ |
| 1142 | Return the earliest object according to fields (if given) or by the |
| 1143 | model's Meta.get_latest_by. |
| 1144 | """ |
| 1145 | if fields: |
| 1146 | order_by = fields |
| 1147 | else: |
| 1148 | order_by = getattr(self.model._meta, "get_latest_by") |
| 1149 | if order_by and not isinstance(order_by, (tuple, list)): |
| 1150 | order_by = (order_by,) |
| 1151 | if order_by is None: |
| 1152 | raise ValueError( |
| 1153 | "earliest() and latest() require either fields as positional " |
| 1154 | "arguments or 'get_latest_by' in the model's Meta." |
| 1155 | ) |
| 1156 | obj = self._chain() |
| 1157 | obj.query.set_limits(high=1) |
| 1158 | obj.query.clear_ordering(force=True) |
| 1159 | obj.query.add_ordering(*order_by) |
| 1160 | return obj.get() |
| 1161 | |
| 1162 | def earliest(self, *fields): |
| 1163 | if self.query.is_sliced: |
no test coverage detected