Serializing booleans correctly when doing dumpdata from a MySQL database using Django 0.96

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
# In django/core/serializers/python.py add these two lines to the handle_field method:

if isinstance(field, models.BooleanField) and isinstance(self._current[field.name], int):
    self._current[field.name] = bool(self._current[field.name])

# Original method

     def handle_field(self, obj, field):
         self._current[field.name] = getattr(obj, field.name)


# Modified method

     def handle_field(self, obj, field):
         self._current[field.name] = getattr(obj, field.name)
         if isinstance(field, models.BooleanField) and isinstance(self._current[field.name], int):
             self._current[field.name] = bool(self._current[field.name])

Comments

(Forgotten your password?)

You may use Markdown syntax here, but raw HTML will be removed.