yamap_55
yamap_55

Reputation: 113

return the value of another table if it is null

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.

example

Hoge.objects.select_related('huga').annotate(piyo=NullIf('null_column', 'huga.name')).all()

Upvotes: 0

Views: 232

Answers (1)

weAreStarsDust
weAreStarsDust

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

Related Questions