skip to Main Content

I want to get a list of users that have provided an email in my database using Django.

This is my query:
list(App.objects.exclude(admins__email__exact='').values('admins__email'))

which strangely excludes everything and gives: []

although when I run: list(App.objects.all().values('admins__email')) i can see a list of user emails and NOT all of them are empty:

[{'admins__email': '[email protected]'},
 {'admins__email': ''},
 {'admins__email': ''},
 {'admins__email': ''}, 
 {'admins__email': '[email protected]'},
 {'admins__email': '[email protected]'},
 {'admins__email': ''},
 ...]

i expect to get this list:

[{'admins__email': '[email protected]'},
 {'admins__email': '[email protected]'},
 {'admins__email': '[email protected]'}]

ps: somehow I can get the correct list with list(User.objects.exclude(email__exact='').values('email')) but in this context, I need to apply some filters to my App and therefore I need to get the first query to work.

2

Answers


  1. Chosen as BEST ANSWER

    I finally got it working this way:

    list(App.objects.filter(admins__email__regex='.').values('admins__email'))
    

    although I find it odd that __exact='' is not working. seems like a Django bug to me.


  2. Try this

    User.objects.exclude(email__isnull=True).exclude(email__exact='')
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search