ecogels / tds-django

SQL Server backend for django >= 3.2

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

error with encoding gb18030

cnanyi opened this issue · comments

commented

I use django 3.2.10 with tsd-django driver, the table data in Sqlserver2017 is wrong value:
id: 1, name: '常'

this is the Traceback in django admin:

Unicode error hint
The string that could not be encoded/decoded was: ����


Traceback (most recent call last):
  File "/Users/anyi/.local/share/virtualenvs/myapp/lib/python3.9/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/Users/anyi/.local/share/virtualenvs/myapp/lib/python3.9/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/anyi/.local/share/virtualenvs/myapp/lib/python3.9/site-packages/django/contrib/admin/options.py", line 616, in wrapper
    return self.admin_site.admin_view(view)(*args, **kwargs)
  File "/Users/anyi/.local/share/virtualenvs/myapp/lib/python3.9/site-packages/django/utils/decorators.py", line 130, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/Users/anyi/.local/share/virtualenvs/myapp/lib/python3.9/site-packages/django/views/decorators/cache.py", line 44, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "/Users/anyi/.local/share/virtualenvs/myapp/lib/python3.9/site-packages/django/contrib/admin/sites.py", line 232, in inner
    return view(request, *args, **kwargs)
  File "/Users/anyi/.local/share/virtualenvs/myapp/lib/python3.9/site-packages/django/utils/decorators.py", line 43, in _wrapper
    return bound_method(*args, **kwargs)
  File "/Users/anyi/.local/share/virtualenvs/myapp/lib/python3.9/site-packages/django/utils/decorators.py", line 130, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/Users/anyi/.local/share/virtualenvs/myapp/lib/python3.9/site-packages/django/contrib/admin/options.py", line 1815, in changelist_view
    'selection_note': _('0 of %(cnt)s selected') % {'cnt': len(cl.result_list)},
  File "/Users/anyi/.local/share/virtualenvs/myapp/lib/python3.9/site-packages/django/db/models/query.py", line 262, in __len__
    self._fetch_all()
  File "/Users/anyi/.local/share/virtualenvs/myapp/lib/python3.9/site-packages/django/db/models/query.py", line 1324, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "/Users/anyi/.local/share/virtualenvs/myapp/lib/python3.9/site-packages/django/db/models/query.py", line 51, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
  File "/Users/anyi/.local/share/virtualenvs/myapp/lib/python3.9/site-packages/django/db/models/sql/compiler.py", line 1208, in execute_sql
    return list(result)
  File "/Users/anyi/.local/share/virtualenvs/myapp/lib/python3.9/site-packages/django/db/models/sql/compiler.py", line 1646, in cursor_iter
    for rows in iter((lambda: cursor.fetchmany(itersize)), sentinel):
  File "/Users/anyi/.local/share/virtualenvs/myapp/lib/python3.9/site-packages/django/db/models/sql/compiler.py", line 1646, in <lambda>
    for rows in iter((lambda: cursor.fetchmany(itersize)), sentinel):
  File "/Users/anyi/.local/share/virtualenvs/myapp/lib/python3.9/site-packages/django/db/utils.py", line 97, in inner
    return func(*args, **kwargs)
  File "/Users/anyi/.local/share/virtualenvs/myapp/lib/python3.9/site-packages/pytds/__init__.py", line 868, in fetchmany
    row = self.fetchone()
  File "/Users/anyi/.local/share/virtualenvs/myapp/lib/python3.9/site-packages/pytds/__init__.py", line 853, in fetchone
    row = self._session.fetchone()
  File "/Users/anyi/.local/share/virtualenvs/myapp/lib/python3.9/site-packages/pytds/tds.py", line 1581, in fetchone
    if not self.next_row():
  File "/Users/anyi/.local/share/virtualenvs/myapp/lib/python3.9/site-packages/pytds/tds.py", line 1592, in next_row
    self.process_token(marker)
  File "/Users/anyi/.local/share/virtualenvs/myapp/lib/python3.9/site-packages/pytds/tds.py", line 1542, in process_token
    return handler(self)
  File "/Users/anyi/.local/share/virtualenvs/myapp/lib/python3.9/site-packages/pytds/tds.py", line 1660, in <lambda>
    tds_base.TDS_NBC_ROW_TOKEN: lambda self: self.process_nbcrow(),
  File "/Users/anyi/.local/share/virtualenvs/myapp/lib/python3.9/site-packages/pytds/tds.py", line 663, in process_nbcrow
    value = curcol.serializer.read(r)
  File "/Users/anyi/.local/share/virtualenvs/myapp/lib/python3.9/site-packages/pytds/tds_types.py", line 690, in read
    return r.read_str(size, self._codec)
  File "/Users/anyi/.local/share/virtualenvs/myapp/lib/python3.9/site-packages/pytds/tds.py", line 212, in read_str
    return codec.decode(readall(self, size))[0]

Exception Type: UnicodeDecodeError at /admin/users/archive/
Exception Value: 'gbk' codec can't decode byte 0xfc in position 2: illegal multibyte sequence

the byte code of name is:

b'\xb3\xa3\xfc\xc7'

it can be decode as gb18030

>>> b'\xb3\xa3\xfc\xc7'.decode('gb18030')
'常\ue3d2'

so, I use pymssql fetch the same record, the return values:

...
>>>row = cur.fetchone()
>>>row[0], row[1]:
>>>1, '常\ue3d2'

Hello,
I see a similar issue here with pytds, denisenkom/pytds#50 .

Maybe you could try to use a BinaryField and/or a custom field https://docs.djangoproject.com/en/3.2/howto/custom-model-fields/ for storing gb18030 data.