cristhiam
cristhiam

Reputation: 506

QuerySet with Avg

I'm having problems making a queryset for a report :/

The ChangeLog model (please see model picture below) stores every change in MedicalRequest model. I'm trying to get the time average from first record to last record in ChangeLog for a single MedicalRequest. I'm querying from customer because I'need make some reports.

Database models

I already test this:

customer = Customer.objects.get(pk=10)
customer.medicalrequest_set.aggregate(avg=Avg(Max(changelog__timestamp),
  Min(changelog__timestamp)))

but I'm getting 'changelog__timestamp' is not defined :( any idea?

Upvotes: 0

Views: 56

Answers (1)

Ted Klein Bergman
Ted Klein Bergman

Reputation: 9746

Your problem is with your syntax. changelog__timestamp is treated as a variable, which you haven't defined. You have to pass the name as a string to these functions. Like, Max('changelog__timestamp').

Upvotes: 1

Related Questions