1. Mikhail Denisenko
  2. django-pytds
  3. Issues
Issue #1 resolved

Some option for charset

Dmitri Bogomolov
created an issue

Can you provide some option to specify database charset? Or maybe automatic conversion from database charset to UTF-8. Django expects all bytestrings in UTF-8, but database may have 'cp1251' for example.

FreeTDS have a good config option for this:

TDS_Version     = 7.0
ClientCharset   = UTF-8

Comments (7)

  1. Dmitri Bogomolov reporter

    It seems with my database this doesn't work in pytds too. I got all char fields values as bytestrings in 'cp1251' (encoding of database which I cannot change). The code I tried is the simple SELECT query. In django it is something like

    from django.db import models
    class MyModel():
        row_id = models.IntegerField(primary_key=True)
        name = models.CharField(_('name'), db_column='sp242', max_length=64)
        class Meta:
            db_table = 'sc224'
        def __unicode__(self):
            return self.name # or u'%s' % self.name
    

    Then in shell

    $ ./manage.py shell
    ...
    In [1]: from app.models import MyModel
    
    In [2]: MyModel.objects.all()[0]
    Out[2]: <MyModel: [Bad Unicode data]>
    

    Or get DjangoUnicodeDecodeError in djano admin.

    Maybe I can post the results of some test? con.tds_version == pytds.TDS73B for me.

  2. Mikhail Denisenko repo owner

    I tried this code it worked fine:

    412 class CharTable(models.Model):$
    413     row_id = models.IntegerField(primary_key=True)$
    414     name = models.CharField('name', db_column='sp242', max_length=64)$
    415 $   
    416     def __unicode__(self):$
    417         return self.name$
    418 $       
    419 $
    420 class CharTestCase(TestCase):$
    421     def runTest(self):$
    422         CharTable.objects.create(row_id=1, name='test')$
    423         print CharTable.objects.all()[0]$
    424         print type(CharTable.objects.all()[0].name)$
    

    Creating test database for alias 'default'...$ .......test$ <type 'unicode'>$ ...........$

    I tried on MSSQL 2008. Can you send me db schema for this table? I suspect that field type is binary in your case.

  3. Log in to comment