Opened 17 years ago
Closed 17 years ago
#6738 closed (invalid)
Cannot get objects when an attribute is None
Reported by: | Owned by: | nobody | |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
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 |
Description
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\manager.py", line 69, in get return self.get_query_set().get(*args, **kwargs) File "C:\Python25\lib\site-packages\django\db\models\query.py", line 263, in g et raise self.model.DoesNotExist, "%s matching query does not exist." % self.mo del._meta.object_name 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:
Reported.objects.get(link=Link.objects.get(id=2))
Note:
See TracTickets
for help on using tickets.
This query should be performed using isnull: http://www.djangoproject.com/documentation/db-api/#isnull