В чем разница между queryset last() и latest() в django?

Я хочу получить данные, которые были вставлены последними, поэтому я использовал код django

user = CustomUser.objects.filter(email=email).last()

так что это дает мне данные последнего пользователя

но затем экспериментально я использовал

user = CustomUser.objects.filter(email=email).latest()

то он не дал мне объект пользователя. Теперь, в чем разница между earliest(), latest, first и last()?

There are several differences between .first() [Django-doc]/.last() [Django- doc] and .earliest(…) [Django-doc]/.latest(…) [Django-doc]. The main ones are:

  • .first() and .last() do not take field names (or orderable expressions) to order by, they do not have parameters, .earliest(…) and .latest(…) do;
  • .first() and .last() will work with the ordering of the queryset if there is one, .earliest(…) and .latest(…) will omit any .order_by(…) clause that has already been used;
  • if the queryset is not ordered .first() and .last() will order by the primary key and return the first/last item of that queryset, .earliest(…) and .latest(…) will look for the get_latest_by model option [Django-doc] if no fields are specified; and
  • .first() and .last() will return None in case the queryset is empty; whereas .earliest(…) and .latest(…) will raise a DoesNotExist exception [Django-doc].
Вернуться на верх