Django South incorrectly parsing python keywords

Nov 26, 2014 | Comments

Code base below is an expert from a production level Django stack using Django South 0.8.4 as a database migration tool.

Came across a bloc of code on Django Models looking like this earlier:


The category class inherits the django model class and extends a field called ‘type’ as a varchar field in SQL database.

See the problem? The variable type is a python keyword and should never be declared as a variable field in a python module. I would expect a SyntaxError be thrown when South parses the file for migration but it does not. As it turns out, the migration file is actually parsed correctly as ‘intended’.


I went to dig up the south documentation API that handles this:


As seen, South simply parses the field_name as a string without performing any form of python keyword checking on the input. This is not right and should be changed. Not only does it confuse the developer but could potentially corrupt the database. With the current implementation, we could potentially have field names like ‘-—____’, ‘%*@#^’ or even ‘:)’. Imagine the kind of SQL statments you have to write to retrieve those fields :D.

Until South do something about this, we should consciously avoid using python keywords even if it works for the migration. As a quick hack, run

python <filename>.py

to quickly check for any SyntaxErrors in the file before further actions.

web development