#27841 closed New feature (duplicate)

ORM 'join on' with extra restrictions

Reported by: Brett Huff Owned by: nobody
Component: Database layer (models, ORM) Version: 1.10
Severity: Normal Keywords: QuerySet.extra
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

My use case is a budget app. I have the following:

class Category

class Transaction
date: date
amount: decimal
category: manytomany(Category, through=Split)

I need to get all categories with the amount spent in a month, even if no money was spent in that category for the month. My SQL solution is to "LEFT OUTER JOIN transaction ON (split.transaction_id = transaction.id and date between <earliest> and <latest>)", then I can get the sum of "transactionamount". Every Django ORM solution I've tried will return only the categories with at least one transaction during the month, so I'm missing a bunch of categories when the month starts.

I'm going to use extra or raw until I can figure out a better way to get the results I want. I was told (by https://docs.djangoproject.com/en/1.10/ref/models/querysets/) to open a ticket to tell you about my legitimate usage.

Change History (2)

comment:1 Changed 11 months ago by Brett Huff

Keywords: QuerySet.extra added

comment:2 Changed 11 months ago by Simon Charette

Resolution: duplicate
Status: newclosed

Duplicate of #27332.

Note: See TracTickets for help on using tickets.
Back to Top