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
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. Template tag - list punctuation for a list of items by shapiromatron 10 months, 3 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 7 months ago

Comments

Please login first before commenting.