I have a very similar query that works for two different types of inputs and will match on the correct column. Both queries are essentially the same except for one word, i.e. the column name.
def trade_company_group(company_group):
# can filter both name or alias as name will always contain 'Companies'
if "COMPANIES" in company_group.upper():
return (
// Same query as below except for "name", below is "alias"
Q(buy_book__entity__company_groups__name__iexact=company_group) &
Q(sell_book__entity__company_groups__name__iexact=company_group) &
(
~Q(buy_book__entity__type=ENTITY.INTERNAL) |
(
Q(buy_book__entity__primary_company_group__name__iexact=company_group) |
Q(sell_book__entity__primary_company_group__name__iexact=company_group)
)
))
return (
Q(buy_book__entity__company_groups__alias__iexact=company_group) &
Q(sell_book__entity__company_groups__alias__iexact=company_group) &
(
~Q(buy_book__entity__type=ENTITY.INTERNAL) |
(
Q(buy_book__entity__primary_company_group__alias__iexact=company_group) |
Q(sell_book__entity__primary_company_group__alias__iexact=company_group)
)
))
I don't want to duplicate code so I was hoping there was a way to substitute the column name in the query depending on my if statement.
Is this possible?
You can use a dict instead
.e.g This:
Q(buy_book__entity__type=ENTITY.INTERNAL)
Is equivalent to this:
q_filter = {"buy_book__entity__type": ENTITY.INTERNAL}
Q(**q_filter)