Set the database charset for django inspectdb
I'm starting to use django, which is looking like a really excellent web application framework, to build an application with a legacy database data model. It comes with a really promising tool for this purpose: "inspectdb". You just set your database connection options, run "python manage.py inspectdb", and it outputs a really good start at a data model for your database.
I had two problems:
1. The character fields came out 3 times as large as they were in the database.
2. Some CHAR fields came out as TextField, not CharField.
The solution to #1 is to set the charset properly when talking to the database. This database has lived since 1975 (ok, not really, but it seems like it), so obviously isn't in Unicode. Django 0.96's defaults when talking to the database include charset: 'utf8'. MySQLdb seems to include padding for UTF-8 character expansion. To work around this, I put the following in my settings.py:
#2 looks like an oversight in django's mysql backend - it knows to translate VARCHAR to CharField, but CHAR it allows to default to TextField. A simple patch, submitted to django's trac as #4048, fixes this to behave as I hoped.
I had two problems:
1. The character fields came out 3 times as large as they were in the database.
2. Some CHAR fields came out as TextField, not CharField.
The solution to #1 is to set the charset properly when talking to the database. This database has lived since 1975 (ok, not really, but it seems like it), so obviously isn't in Unicode. Django 0.96's defaults when talking to the database include charset: 'utf8'. MySQLdb seems to include padding for UTF-8 character expansion. To work around this, I put the following in my settings.py:
DATABASE_OPTIONS={'charset': 'Latin1',}
#2 looks like an oversight in django's mysql backend - it knows to translate VARCHAR to CharField, but CHAR it allows to default to TextField. A simple patch, submitted to django's trac as #4048, fixes this to behave as I hoped.

0 Comments:
Post a Comment
<< Home