django query field is null

  1. Import the necessary modules:
from django.db.models import F, ExpressionWrapper, fields
from django.db.models.functions import Coalesce
  1. Use the annotate function to add a new field that represents whether the desired field is null or not. Replace YourModel and your_field with your actual model and field names:
YourModel.objects.annotate(is_field_null=ExpressionWrapper(
    Coalesce('your_field', F('your_field')), 
    output_field=fields.BooleanField()
))
  1. Filter the queryset based on the newly added field to get only the rows where the field is null:
YourModel.objects.filter(is_field_null=True)