pythondjangodjango-modelsdjango-viewsmod-python

Inserting language characters in Django


I am following the link http://code.google.com/p/django-multilingual-model/ Basically i am trying to insert hindi characters into mysql db

I have created the files in the test directory as in the above said link and using django version 1.1 an dpython version is 2.4.3

I geta error message as the following.

  from testlanguages import Language,BookTranslation,Book
  >>> lang_pl = Language(code="pl", name="Polish")
  >>> lang_pl.save()
  >>> book_pl.language = lang_pl
  >>> book_pl.save()
  >>> lang_hi = Language(code="hi", name="Hindi")
  >>> lang_hi.save()
  >>> book_hi = BookTranslation()
  >>> book_hi.title = "का सफल प्रक्षेपण किया है. इस राकेट ने पाँच"  Sum characters as in bbc.co.uk/hindi
  >>> book_hi.description = "का सफल प्रक्षेपण किया है. इस राकेट ने पाँच" Sum characters as in bbc.co.uk/hindi
  >>> book_hi.model = book
  >>> book_hi.language = lang_hi
  >>> book_hi.save()
  Traceback (most recent call last):
    File "<console>", line 1, in ?
    File "/opt/project/django/django/db/models/base.py", line 410, in save
    self.save_base(force_insert=force_insert, force_update=force_update)
    File "/opt/project/django/django/db/models/base.py", line 495, in save_base
    result = manager._insert(values, return_id=update_pk)
    File "/opt/project/django/django/db/models/manager.py", line 177, in _insert
    return insert_query(self.model, values, **kwargs)
    File "/opt/project/django/django/db/models/query.py", line 1087, in insert_query
    return query.execute_sql(return_id)
    File "/opt/project/django/django/db/models/sql/subqueries.py", line 320, in execute_sql
    cursor = super(InsertQuery, self).execute_sql(None)
    File "/opt/project/django/django/db/models/sql/query.py", line 2369, in execute_sql
    cursor.execute(sql, params) 
    File "/opt/project/django/django/db/backends/util.py", line 19, in execute
    return self.cursor.execute(sql, params)
    File "/opt/project/django/django/db/backends/mysql/base.py", line 84, in execute
    return self.cursor.execute(query, args)
    File "/usr/lib/python2.4/site-packages/MySQLdb/cursors.py", line 165, in execute
    self._warning_check()
    File "/usr/lib/python2.4/site-packages/MySQLdb/cursors.py", line 80, in _warning_check
    warn(w[-1], self.Warning, 3)
    File "/usr/lib/python2.4/warnings.py", line 61, in warn
    warn_explicit(message, category, filename, lineno, module, registry)
    File "/usr/lib/python2.4/warnings.py", line 96, in warn_explicit
        raise message
        Warning: Incorrect string value: '\xE0\xA4\x95\xE0\xA4\xBE...' for column 'title' at row 1

How to resolve this.

Is there any easy method to insert the special characters into DB


Solution

  • This is most likely a MySQL problem, which doesn't always support Unicode characters by default.

    You should probably declare the DEFAULT CHARSET to utf8 and DEFAULT COLLATION to utf8_general_ci on your database (use the ALTER DATABASE or modify the CREATE DATABASE statements, as explained here).