Login

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

Author:
chrj
Posted:
July 29, 2008
Language:
Python
Version:
.96
Tags:
dumpdata 0.96 seralization
Score:
0 (after 0 ratings)

Django 0.96 seems to have a bug when serializing from MySQL. BooleanFields are encoding as 0/1 instead of true/false. Hacking the python serializer seems to fix that.

The bug shows up as (fx. when using loaddata on a dump from MySQL in PostgreSQL):

Problem installing fixture '/tmp/data.json': ERROR:  column "is_staff" is of 
type boolean but expression is of type integer
HINT:  You will need to rewrite or cast the expression.
 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])

More like this

  1. CSV serializer by stringify 4 years, 4 months ago
  2. Export Models by brunobord 6 years, 8 months ago
  3. Command to make fixtures. by buriy 6 years, 7 months ago
  4. GeoJSON Serializer for GeoDjango (gis) by danielsokolowski 3 years, 9 months ago
  5. Updated: GeoJSON Serializer for GeoDjango (gis) by danielsokolowski 3 years, 3 months ago

Comments

Please login first before commenting.