Reputation: 113
I've found that in a Django query, if one column is null, you can use django.db.models.functions.NullIf
to return the value of another column.
Hoge.objects.annotate(piyo=NullIf('null_column', 'column_name')).all()
But I want to know how to return the value of another table.
Hoge.objects.select_related('huga').annotate(piyo=NullIf('null_column', 'huga.name')).all()
Upvotes: 0
Views: 232
Reputation: 2752
You should use __
instead of .
and add output_field
if your values contains mixed types
Hoge.objects.annotate(piyo=NullIf('null_column', 'huga__name', output_field=CharField()))
Upvotes: 1