How would I do:
FinancialStatements.objects.get(statement_id=statement_id)
or SalesStatements.objects.get(statement_id=statement_id)
The result will always yield one result.
I ended up using the try/except route here:
try:
statement_object = FinancialStatements.objects.get(statement_id=statement_id)
except FinancialStatements.DoesNotExist:
statement_object = SalesStatements.objects.get(statement_id=statement_id)
Why not simply do:
This should work, because
filterreturns a list – and an empty list if no entries match. An empty list evaluates to false in python’s boolean logic, e.g. try running:(Just as a check, compare
print ["Hi"] or "hello")So, if the first query returns empty, the second will then be run. However, if the first matches anything, this will be result and the second query will be ignored.
Addendum: result will then be of a list type – you’ll need to extract the (one and only) element with
result[0].