Maybe something like this should work:
Terms.objects.annotate(
transactions_sum=models.Subquery(
Transaction.objects.filter(
terms=models.OuterRef("pk"),
)
# Now In order to filter by some date_range
# which vary by timezone, you would want to
# annotate the date field with the applied timezone
# something like
# .annotate(tz_date=Func('date', Value('GMT'),
models.OuterRef("timezone"), function='CONVERT_TZ'))
# .filter(tz_date__range=(min_date, max_date))
.values('terms')
.annotate(amount_sum=models.Sum("amount"))
.values('amount_sum'),
output_field=models.IntegerField(),
)
)
On Thursday, January 3, 2019 at 7:32:55 PM UTC+2, Dustin Wyatt wrote:
>
> Given the following models:
>
>
> class Transaction(models.Model):
> amount = models.IntegerField()
> date = models.DateField()
> terms = models.ForeignKey('Terms')
>
>
> class Terms(models.Model):
> name = models.CharField(max_length=100)
> timezone = models.CharField(max_length=50)
>
>
> I need to annotate Terms with the sum of the related Transaction.amount's
> where the Transaction.date is between two dates and what those two dates
> are varies by Terms.timezone.
>
> Possible?
>
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/6b90d384-79f4-48b1-bfff-5bde637a7155%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.