masnun
masnun

Reputation: 11916

Unicode error in Django

I am trying to implement CRUD in a database that will contain unicode strings. Here are the models:

from django.db import models


class Category(models.Model):
    cat_name = models.CharField(max_length=100, verbose_name="Category Name")

    def __unicode__(self):
        return self.cat_name

class Translation(models.Model):
        english_term = models.TextField(verbose_name="English Term")
        bangla_term = models.TextField(verbose_name="Bangla Term")
        bangla_variant = models.TextField(verbose_name="Variant")
        parts_of_speech = models.TextField(verbose_name="Parts of Speech")
        category = models.ForeignKey(Category, verbose_name="Category")

        def __unicode__(self):
            return self.english_term

Django by default created the database and tables with latin-swedish-ci or something. I have manually converted the database and the tables to utf8-unicode-ci (I apologize, I know I got the collation names wrong).

I have registered the models in my admin area. When I attempt to add any entries into the models, I get an error:

Warning at /admin/glossary/category/add/

Incorrect string value: '\xE0\xA6\x97\xE0\xA6\xA8...' for column 'cat_name' at row 1

Any help?

Regards

Upvotes: 0

Views: 406

Answers (2)

okm
okm

Reputation: 23871

You may want to recreate DB with utf8 encoding or check configured encoding on both client and server side. Django does not create database, its your responsibility to create one and set correct parameters in Django settings for Django to connect. The only exception is sqlite3 backend, which always using utf8.

Upvotes: 1

Andrew Sledge
Andrew Sledge

Reputation: 10351

Try

def __unicode__(self):
    return u'%s' % self.cat_name

in your Category model.

Upvotes: 0

Related Questions