Usman Khan
Usman Khan

Reputation: 686

Django - Using a string variable in a Q filter

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?

Upvotes: 0

Views: 1315

Answers (2)

Cagatay Barin
Cagatay Barin

Reputation: 3496

You can apply operations like & and | on Q objects. Here is a simple example for you to simplify the query.

query = Q(buy_book__entity__company_groups__name__iexact=company_group) & Q(sell_book__entity__company_groups__name__iexact=company_group)

if "COMPANIES" in company_group.upper():
    query &= (
                ~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)
                )
            )
else:
    query &= (
        ~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)
        )
    )

Upvotes: 0

Kedar
Kedar

Reputation: 1698

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)

Upvotes: 4

Related Questions