I'm running metrics on user data and want exclude users that have bogus emails like '@example.com' or '@test.com'.
I tried
emails_to_exclude = ['@example.com', '@test.com', '@mailinator.com' ....]
Users.objects.exclude(email__endswith__in=emails_to_exclude)
Unfortunately this doesn't work. Looks like endswith
and in
don't play nice with each other. Any ideas?
Simply loop over the QuerySet, as QuerySets are lazy.
emails_to_exclude = ['@example.com', '@test.com', '@mailinator.com' ....]
users = Users.objects
for exclude_email in emails_to_exclude:
users = users.exclude(email__endswith=exclude_email)
users = users.all()
You can also do this with regular expressions in single query.
emails_to_exclude = ['@example.com', '@test.com', '@mailinator.com' ....]
User.objects.exclude(email__regex = "|".join(emails_to_exclude))
I don't know the efficiency of this query.
This will not work for SQLite, as it has no built in regular expression support.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With