Micah Pearce
Micah Pearce

Reputation: 1935

Django model QuerySet not returning when filtered

I'm not really sure what's happening here. I can see that I have a couple of emails in my database, but I can't seem to filter them. for example, when I run

qs1 = EmailActivation.objects.all()
>>> print(qs1)
<EmailActivationQuerySet [<EmailActivation: [email protected]>, <EmailActivation: [email protected]>]>

however, when I run the following I get nothing

qs2 = EmailActivation.objects.all().filter(email='[email protected]')
>>> print(qs2)
<EmailActivationQuerySet []>

my model looks like this:

class EmailActivation(models.Model):
    user = models.ForeignKey(User)
    email = models.EmailField()

I'd expect my qs2 to return '[email protected]' since it is in the database as seen by qs1. Does someone see what I'm doing wrong?

Thanks,

Edit: looking closer I see the following:

qs2 = 

EmailActivation.objects.all().filter(email__icontains='[email protected]')
>>> print(qs2)
<EmailActivationQuerySet [<EmailActivation: [email protected]>]>

Does this mean that there is some whitespace or hidden character in my 'email'? I imagine filtering with icontains would be bad as someone could be '[email protected]'. What could I do to strip whatever hidden character is there?

Upvotes: 0

Views: 76

Answers (1)

webbyfox
webbyfox

Reputation: 1069

If you explicitly specify exact match on the filter then you should get a result what you are after. I don't think so there are any hidden characters here...

>>> EmailActivation.objects.all().filter(email__exact='[email protected]')
<QuerySet [<EmailActivation: EmailActivation object (1)>]>

Upvotes: 1

Related Questions