Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to filter empty value in a Django queryset?

I want to filter out empty value on email column.

So I tried this:

users = User.objects.filter(email__iexact='')

But, record has empty value would not be appeared.

Anyone know why is that?

Model is:

class User(models.Model):
    email = models.EmailField(blank=True)
like image 456
richasonson Avatar asked Jan 01 '26 13:01

richasonson


1 Answers

If you want to display all the emails that are not empty (filter-out empty) then you can exclude them using

users = User.objects.exclude(email__isnull=True)

If you want to get all the columns with empty email value then use

users = User.objects.filter(email__isnull=True)
like image 172
Demetris Avatar answered Jan 04 '26 01:01

Demetris



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!