logo4 Evolution is progress—                          
progress is creativity.        

Programming Django Is Boresome And Error Prone

view blog view wiki view wiki view wiki

If you know what django is about it is easy to understand the documentation, but it needs some time and several trial-and-error experiments till you really understand the philosophy. Not before you understood the philosophy you clearly know what tasks can be easily solved with django and what task become even more complicated when solved with django.

I'll give an example that cost me a lot of time to figure it out. Although the django documentation says that a boolean model field is represented as boolean form field (see https://docs.djangoproject.com/en/dev/topics/forms/modelforms/), but that's true. The boolean form field is correctly represented as a boolean field. It only yields True or False, but at least in MySQL the boolean model field is represented by a tinyint(1) where 1 represents True and 0 False. That wouldn't pose any problem, but the Model.save() function does not save the boolean values but only integer values.

So it is necessary to translate in a models save routine.

    def save(self, *args, **kwargs):
        self.boolField = 1 if self.boolField else 0
        super(MyModel, self).save(*args, **kwargs) # Call the "real" save() method.

Well this routine works, but I realized after some additional trials that it works as well without such a translation into a tinyint. My mistake was to put a comma after each statement when I transferred the the cleaned data array. Python did not produce an error but did not save the work to the model either.

At least I learned a lot about how to debug django.


(c) Mato Nagel, Weißwasser 2004-2013, Disclaimer