I “inherited” a database from an old system we have, in which (among other ugly things) there’s a column in a certain table that should be boolean, instead it is a varchar(1) with ‘N’ or ‘0’ to represent false, and ‘Y’ or ‘1’ to represent true (different parts of the code using one or the other convention when writing, but apparently all of them are able to recognize both when reading).
I’m porting/rewriting the system, originally written in Java, to Python+Django, and I’ll be able to shut down the old one and “clean” the database mess only when the port is complete.
I’d like to make the field in the model class to appear externally as a boolean, so when the database is finally corrected, the only thing I’ll have to do in the code is to change the field to a plain BooleanField. Is there a simple way to do that?
You can create your own field class that act like you want by subclassing django.db.models.Field and by implementing the to_python method e.g.
Hope this will give you some insight 🙂