Opened 11 years ago

Closed 11 years ago

#6738 closed (invalid)

Cannot get objects when an attribute is None

Reported by: John <john@…> Owned by: nobody
Component: Database layer (models, ORM) Version: master
Severity: Keywords: db, error, data corruption
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


I have a model defined like this:

class Reported(models.Model):
    user = models.ForeignKey(User, null=True)
    link = models.ForeignKey(Link)
    date = models.DateTimeField(auto_now_add=True)

but when I execute this on the shell, it gives an erroneous result.

>>> Reported.objects.create(user=None, link=Link.objects.get(id=2))
<Reported: Reported object>
>>> Reported.objects.get(user=None, link=Link.objects.get(id=2))
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\Python25\lib\site-packages\django\db\models\", line 69, in
    return self.get_query_set().get(*args, **kwargs)
  File "C:\Python25\lib\site-packages\django\db\models\", line 263, in g
    raise self.model.DoesNotExist, "%s matching query does not exist." %
DoesNotExist: Reported matching query does not exist.

As you can see, I first create an object, and then search for it, but the just-created item cannot be found! This only happens when one of the search fields is None, the following works:


Change History (1)

comment:1 Changed 11 years ago by Alex Gaynor

Resolution: invalid
Status: newclosed

This query should be performed using isnull:

Note: See TracTickets for help on using tickets.
Back to Top