Blog

The Digital Agency for International Development

Django QuerySet silently allows querying with objects of wrong class

By Chris Wilson on 11 January 2013

I discovered a bug with how Django's database query builder handles queries using objects of the wrong type. It silently uses just the object's primary key (ID) instead of throwing an exception.

I reported the bug, but I failed to find a previous report of the same bug (16955). There is a patch on that ticket, but it's not yet merged into Django, and it's not clear when or whether it will be.

In the mean time, I used that patch to build a new monkey patch that allows us to catch this error in our own code.

Did I mention that I love monkey patching?