[Answer]-Validate Django Choices with Accents in ModelForms

1👍

Note that in the CHOICES array, your entries are supposed to be (code, label). The code is what Django actually uses internally and in the DB, whereas the label is purely presentational.

Here, it would make sense for you to follow that convention. Among other things, this will make internationalizing your project easier down the road (if needed). Incidentally, it should also make your problem go away:

CHOICES = (('graduated', 'Graduación'),
           ('temporary', 'Baja Temporal'),
           ('expelled', 'Expulsión'))

Note that if you already have data in your DB, you’re going to have to somehow migrate it.


Now, depending on which version of Python you’re using, it’s also a good idea to ensure your non-ASCII strings are declared as unicode (otherwise, you’re leaving it up to Python to guess their encoding at runtime).

Specifically, in Python 2, you should do the following (in Python 3, you don’t need to do anything):

CHOICES = (('graduated', u'Graduación'),
           ('temporary', u'Baja Temporal'),
           ('expelled', u'Expulsión'))

Also, make sure you to declare the encoding of your file. The first line should be:

#coding:utf-8

Note that this assumes that your file is encoded in utf-8, but that’s a pretty safe assumption.

0👍

I am using python 2.7, and i resolved it adding an ‘u’ to the strings, for example

u’Explicación’

Applied to labels

labels = {
            'anio': _(u'Escriba el año'),
            'tarjetacirculacion': _(u'# de tarjeta de circulación'),
        }

Leave a comment