I have the following models:
class Foo:
amount = DecimalField()
identifier = CharField()
class FooMapping:
# This is not id and can be used on other models aside from Foo
identifier = CharField()
condition = CharField()
default_value = CharField()
condition_value = CharField()
I wanted to filter on the subquery like:
subquery = FooMapping.objects.filter(identifier=OuterRef('identifier')\
.annotate(final_value=Case(
When(
condition='If positive',
OuterRef('amount') > 0,
then=F('condition_value')
),
When(
condition='If negative',
OuterRef('amount') < 0,
then=F('condition_value')
),
default=F('default_value')
)
)
Is this possible in some way? Currently I'm annotating and filtering on Foo for the condition values as a workaround.