Home > Back-end >  How do I get the an object by name and highest timestamp
How do I get the an object by name and highest timestamp

Time:11-12

I want to fetch the transaction belonging to external_id=1 and has the highest timestamp.

I have tried this

max_rating = Transaction.objects.aggregate(organisation_id_from_partner=organisation_id, highest_timestamp=Max('timestamp'))

But I get TypeError: QuerySet.aggregate() received non-expression(s): 1.

CodePudding user response:

Inside aggregate, you can only write Aggregate function. Try this

max_rating = Transaction.objects.filter(organisation_id_from_partner=organisation_id).order_by('-timestamp').first()

CodePudding user response:

You can slightly shorten the answer by @shafik by using .latest(…) [Django-doc]:

max_rating = Transaction.objects.filter(
    organisation_id_from_partner=organisation_id
).latest('timestamp')
  • Related