David542
David542

Reputation: 110163

Get whether a field is NULL or not

I have the following query:

territories = Territory.objects.values_list(
              'territory_id', 'territory__name', 'hd_retail_price__isnull'
             ).order_by('territory__name').distinct()

However, django raises an error that hd_retail_price__isnull is not a field (which it is not). Is there a way to get a boolean on whether the field is null or not? Otherwise my distinct won't work.

I want to have:

SELECT territory_id, territory_name, ISNULL(hd_retail_price) FROM territory

Upvotes: 1

Views: 58

Answers (1)

David542
David542

Reputation: 110163

Use .extra:

territories = Territory.objects.extra(
    select={'has_hd': "ISNULL(hd_retail_price)"}
  ).values_list(
    'territory_id', 'territory__name', 'has_hd'
  ).order_by('territory__name').distinct()

Upvotes: 1

Related Questions