Anjayluh
Anjayluh

Reputation: 1786

How do I get the an object by name and highest timestamp

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.

Upvotes: 2

Views: 228

Answers (2)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476574

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')

Upvotes: 1

shafikshaon
shafikshaon

Reputation: 6404

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()

Upvotes: 2

Related Questions