Skip to content

Django Deserializer Bug On Foreign Key When None

by Paul Kenjora on February 18th, 2008

THis is more for myself than anyone else but remind me to submit this to the bug list. Or at least check if it exists already. When deserializing a model with a foreign key whose value is set to None I get the following exeption:

The Bug

The Fix

I needed a fix quick and I’m not sure this is the best way to do it. The error is at:

The code was attempting to do an object lookup on a key value of None. The exception only checks for does not exist but for some reason the code was returning them all. I added a check to see if the value of the key is None to prevent a lookup in the first place. The following fixed the issue:

  • Nobody
    This is old news, I know - but is the foreign key defined as "blank=True" or "null=True"?

    No doubt you've long since solved the problem, but for some reason I'd defined a ForeignKey with blank=True instead of null=True. My tests were failing with "doesNotExist" and it took me a whole day to solve it.

    Now I get to sit in the corner with the Dunce cap and cry.
blog comments powered by Disqus