On Sun, May 23, 2010 at 10:10 PM, vjimw <[email protected]> wrote:

> I have been reading up on Unicode with Python and Django and I think I
> have my code set to use UTF8 data when saving or updating an object
> but I get an error on model.save()
>
> My database and all of its tables are UTF8 encoded with UTF8 collation
> (DEFAULT CHARSET=utf8;)
> The data I am inputting is unicode
> (u'Save up to 25% on your online order of select HP LaserJet\x92s')
> <type 'unicode'>
>
> But when I try to save this data I get an error
> Incorrect string value: '\\xC2\\x92s' for column 'title' at row 1
>


This error implies that your MySQL table is not set up the say you think it
is, with a charset of utf8. Given a table that actually has a utf8 charset:

k...@lbox:~/software/web/playground$ mysql -p Play2
Enter password:
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5852
Server version: 5.0.67-0ubuntu6.1 (Ubuntu)

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> show create table ttt_tag;
+---------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table   | Create
Table
|
+---------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| ttt_tag | CREATE TABLE `ttt_tag` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(88) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 |
+---------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

I can create an object in Django using the odd unicode character your
string  includes (though I'm not sure what it is supposed to be -- based on
its placement I'd guess it is supposed to be a registered trademark symbol
but that's not what you actually have):

k...@lbox:~/software/web/playground$ python manage.py shell
Python 2.5.2 (r252:60911, Jan 20 2010, 23:16:55)
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from ttt.models import Tag
>>> t = Tag.objects.create(name=u'HP LaserJet\x92s')
>>> print t
HP LaserJet s
>>> quit()

So that works, though the character does not print as anything useful.

If I change the table to have a charset of latin1 (MySQL's default):

mysql> drop table ttt_tag;
Query OK, 0 rows affected (0.00 sec)
mysql> create table ttt_tag (id int(11) not null auto_increment, name
varchar(88) not null, primary key (id)) engine=myisam default charset
latin1;
Query OK, 0 rows affected (0.01 sec)

I can then recreate the error you report:

>>> t = Tag.objects.create(name=u'HP LaserJet\x92s')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
[snipped]
  File "/usr/lib/python2.5/warnings.py", line 102, in warn_explicit
    raise message
Warning: Incorrect string value: '\xC2\x92s' for column 'name' at row 1

So I think one problem is that your table is not actually set up the way you
think it is.

Another may be that you data is not really correct either. What you are
showing that you have in your data is this character:

http://www.fileformat.info/info/unicode/char/0092/index.htm

and I suspect what you really want is either of these:

http://www.fileformat.info/info/unicode/char/2122/index.htm
http://www.fileformat.info/info/unicode/char/00ae/index.htm

Either of these would display better than what you have:

>>> u1 = u'LaserJet\u2122'
>>> print u1
LaserJet(tm)
>>> u2 = u'LaserJet\xae'
>>> print u2
LaserJet(R)

Karen
-- 
http://tracey.org/kmt/

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to