I can't use accented letters in my model

81 views
Skip to first unread message

Virgilio Ravagli

unread,
Jun 8, 2022, 3:30:15 PM6/8/22
to Django users
Hi everybody.
I defined a model class, Book, whose field title may contain accented letters.
But in the case, when the instruction save() is executed, I receive an error saying that "You must not use 8-bit bytestrings unless you use a text_factory that can interpret 8-bit bytestrings (like text_factory = str)".

So. my question is: how can I use accented letters ?

Antonis Christofides

unread,
Jun 8, 2022, 7:07:41 PM6/8/22
to django...@googlegroups.com

Please provide the minimum code that reproduces the problem, and the full traceback.

Regards,

Antonis

Virgilio Ravagli

unread,
Jun 9, 2022, 7:27:27 PM6/9/22
to Django users
All right.
class Book(models.Model):
       code = models.PositiveIntegerField(primary_key=True)
       title = models.CharField(max_length=60,blank=False,null=False)

book = Book()
book.code = 1
book.title = 'Verità'
book.save()

the code is really simple

Antonis Christofides

unread,
Jun 9, 2022, 9:17:16 PM6/9/22
to django...@googlegroups.com

Please read my email again.

Antonis Christofides
+30-6979924665 (mobile)
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/cf785c0e-5cfc-4587-b0cd-ea796a62a20bn%40googlegroups.com.

Virgilio Ravagli

unread,
Jun 10, 2022, 7:50:03 AM6/10/22
to Django users
I have surround book.save with a try...catch...; the exception is: You must not use 8-bit bytestrings unless you use a text_factory that can interpret 8-bit bytestrings (like text_factory = str). It is highly recommended that you instead just switch your application to Unicode strings.
Without the try...catch, here is the traceback:
Environment:


Request Method: POST
Request URL: http://localhost:8000/uti/dataLoading/

Django Version: 1.8.5
Python Version: 2.7.10
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'uti')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django.middleware.security.SecurityMiddleware')


Traceback:
File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\RavagliV\eclipse-workspace-saved\librarian\uti\views.py" in datLoading
  34.                 msg = do_dat_loading()
File "C:\Users\RavagliV\eclipse-workspace-saved\librarian\uti\views.py" in do_dat_loading
  79.             book.save()
File "C:\Python27\lib\site-packages\django\db\models\base.py" in save
  734.                        force_update=force_update, update_fields=update_fields)
File "C:\Python27\lib\site-packages\django\db\models\base.py" in save_base
  762.             updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "C:\Python27\lib\site-packages\django\db\models\base.py" in _save_table
  827.                                       forced_update)
File "C:\Python27\lib\site-packages\django\db\models\base.py" in _do_update
  877.         return filtered._update(values) > 0
File "C:\Python27\lib\site-packages\django\db\models\query.py" in _update
  580.         return query.get_compiler(self.db).execute_sql(CURSOR)
File "C:\Python27\lib\site-packages\django\db\models\sql\compiler.py" in execute_sql
  1062.         cursor = super(SQLUpdateCompiler, self).execute_sql(result_type)
File "C:\Python27\lib\site-packages\django\db\models\sql\compiler.py" in execute_sql
  840.             cursor.execute(sql, params)
File "C:\Python27\lib\site-packages\django\db\backends\utils.py" in execute
  79.             return super(CursorDebugWrapper, self).execute(sql, params)
File "C:\Python27\lib\site-packages\django\db\backends\utils.py" in execute
  64.                 return self.cursor.execute(sql, params)
File "C:\Python27\lib\site-packages\django\db\utils.py" in __exit__
  97.                 six.reraise(dj_exc_type, dj_exc_value, traceback)
File "C:\Python27\lib\site-packages\django\db\backends\utils.py" in execute
  64.                 return self.cursor.execute(sql, params)
File "C:\Python27\lib\site-packages\django\db\backends\sqlite3\base.py" in execute
  318.         return Database.Cursor.execute(self, query, params)

Exception Type: ProgrammingError at /uti/dataLoading/
Exception Value: You must not use 8-bit bytestrings unless you use a text_factory that can interpret 8-bit bytestrings (like text_factory = str). It is highly recommended that you instead just switch your application to Unicode strings.

o1bigtenor

unread,
Jun 10, 2022, 10:07:44 AM6/10/22
to django...@googlegroups.com
On Fri, Jun 10, 2022 at 2:50 AM Virgilio Ravagli
<virgilio...@gmail.com> wrote:
>
> I have surround book.save with a try...catch...; the exception is: You must not use 8-bit bytestrings unless you use a text_factory that can interpret 8-bit bytestrings (like text_factory = str). It is highly recommended that you instead just switch your application to Unicode strings.
> Without the try...catch, here is the traceback:
> Environment:
>
>
> Request Method: POST
> Request URL: http://localhost:8000/uti/dataLoading/
>
> Django Version: 1.8.5
> Python Version: 2.7.10

Hmmmmmmmm - - - - not speaking as any kind of expert - - - - but - - - -
Python 2.xx has been deprecated for some time now. Python 3.10 is
current. Likewise Django is now at version 3.2 for long-term support
and is at 4.0.5 for current. AIUI you may find that your issue has already
been addressed.

HTH

Virgilio Ravagli

unread,
Jun 10, 2022, 11:14:56 AM6/10/22
to Django users
I'm bound to use old versions of Python and Django, cannot pass to newer versions in this project

o1bigtenor

unread,
Jun 10, 2022, 12:21:42 PM6/10/22
to django...@googlegroups.com
On Fri, Jun 10, 2022 at 6:15 AM Virgilio Ravagli
<virgilio...@gmail.com> wrote:
>
> I'm bound to use old versions of Python and Django, cannot pass to newer versions in this project
>

https://python.readthedocs.io/en/v2.7.2/howto/unicode.html

maybe that helps

You may also have a severe case of 'can't get there from here' - - -
meaning it may be a no can do.

HTH

Antonis Christofides

unread,
Jun 10, 2022, 1:25:51 PM6/10/22
to django...@googlegroups.com

Hello,

try this:

book.title = u'Verità'

Regards,

Antonis

P.S. Sorry I was a bit harsh yesterday—I had drunk too much beer :-)

Virgilio Ravagli

unread,
Jun 10, 2022, 7:45:46 PM6/10/22
to Django users
Thank you, Antonis, it works !
book.title = u'Verità' doesn't give any errors.

Just another step: suppose that the title with accented letters stays in a variable, say titolo, and I want to write
book.title = titolo. It gives error. How can I do, when the assignment on the right is not a constant ?
Thanks in advance

Virgilio Ravagli

unread,
Jun 12, 2022, 9:07:22 AM6/12/22
to Django users
I found a way: book.title = titolo.decode('unicode_escape')
it works fine

Antonis Christofides

unread,
Jun 14, 2022, 7:45:06 AM6/14/22
to django...@googlegroups.com

Exactly. The important thing to remember here is that, in Python 2, a "string" is actually a list of bytes, whereas a "unicode string" is actually a list of characters (not a list of unicode characters—just a list of characters). The confusion arises because, when Python was created, i.e. in 1990, multi-byte characters were in their infancy, and we mostly assumed that a character and a byte was more or less the same thing.

In Python 3, the Python 2 "strings" were renamed to "bytes", and the Python 2 "unicode strings" were renamed to "strings"—and the syntax for the literals of these types also changed. This made it way better.

More information about all this is in https://djangodeployment.com/2017/06/19/encodings-part-1/.

Ryan Nowakowski

unread,
Jun 14, 2022, 1:38:36 PM6/14/22
to django...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages