Django’s F objects are used to compare two fields of the same model in a query. This is particularly useful when filtering based on relationships between fields within a model, rather than between a field and a constant value.

from django.db.models import F, Q

if value: return queryset.filter( Q(is_buy=True, price_per_share=F(‘company__highest_buy_ticket_pps’)) | Q(is_buy=False, price_per_share=F(‘company__lowest_sell_ticket_pps’)) )