Django:UnicodeEncodeError:'ascii'编解码器无法对位置1中的字符u'\ xe8'进行编码:序数不在范围内(128)

时间:2017-06-16 09:25:21

标签: django encoding

我使用models.py,forms.py等创建了一个新的Django应用Deces。我想为我的数据库中的creater表创建一个Django migration

我遇到了一个ascii错误,我对Traceback不太了解,尤其是错误所在的位置:

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 341, in execute
    django.setup()
  File "/usr/local/lib/python2.7/site-packages/django/__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/usr/local/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "/usr/local/lib/python2.7/site-packages/django/apps/config.py", line 199, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/local/Cellar/python/2.7.12_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/Users/valentinjungbluth/Desktop/Django/DatasystemsEC/Etat_civil/Deces/models.py", line 26, in <module>
    class Acte_Deces(models.Model):
  File "/usr/local/lib/python2.7/site-packages/django/db/models/base.py", line 310, in __new__
    new_class._meta.apps.register_model(new_class._meta.app_label, new_class)
  File "/usr/local/lib/python2.7/site-packages/django/apps/registry.py", line 216, in register_model
    self.do_pending_operations(model)
  File "/usr/local/lib/python2.7/site-packages/django/apps/registry.py", line 412, in do_pending_operations
    function(model)
  File "/usr/local/lib/python2.7/site-packages/django/apps/registry.py", line 391, in apply_next_model
    self.lazy_model_operation(next_function, *more_models)
  File "/usr/local/lib/python2.7/site-packages/django/apps/registry.py", line 403, in lazy_model_operation
    apply_next_model(model_class)
  File "/usr/local/lib/python2.7/site-packages/django/apps/registry.py", line 391, in apply_next_model
    self.lazy_model_operation(next_function, *more_models)
  File "/usr/local/lib/python2.7/site-packages/django/apps/registry.py", line 377, in lazy_model_operation
    function()
  File "/usr/local/lib/python2.7/site-packages/django/db/models/fields/related.py", line 313, in resolve_related_class
    field.do_related_class(related, model)
  File "/usr/local/lib/python2.7/site-packages/django/db/models/fields/related.py", line 374, in do_related_class
    self.contribute_to_related_class(other, self.remote_field)
  File "/usr/local/lib/python2.7/site-packages/django/db/models/fields/related.py", line 915, in contribute_to_related_class
    super(ForeignKey, self).contribute_to_related_class(cls, related)
  File "/usr/local/lib/python2.7/site-packages/django/db/models/fields/related.py", line 707, in contribute_to_related_class
    setattr(cls._meta.concrete_model, related.get_accessor_name(), self.related_accessor_class(related))
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe8' in position 1: ordinal not in range(128)

例如,这是我的models.py文件:

#-*- coding: utf-8 -*-

from django.db import models
from Identity.models import Person
from django.utils.encoding import force_text
from django_countries.fields import CountryField

LIEU_CHOICES = (
    (u'Logement/Domicile', u'Logement/Domicile'),
    (u'Etablissement hospitalié', u'Etablissement hospitalié'),
    (u'Clinique privée', u'Clinique privée'),
    (u'Hospice/Maison de retraite', u'Hospice/Maison de retraite'),
    (u'Voie/Lieu public', u'Voie/Lieu public'),
    ('Autre','Autre'),
)

PARENTE_CHOICES = (
    (u'Père/Mère', u'Père/Mère'),
    (u'Frère/Soeur', u'Frère/Soeur'),
    (u'Epoux/Epouse', u'Epoux/Epouse'),
    (u'Enfant', u'Enfant'),
    ('Autre','Autre'),
)


class Acte_Deces(models.Model):

    fk_defunt = models.ForeignKey(Person, related_name=u'Défunt(e)', verbose_name=u'Défunt(e)', null=False)
    fk_defunt_epouse = models.ForeignKey(Person, related_name=u'Epouse Défunt(e)', verbose_name=u'Epouse Défunt(e)', null=True)

    fk_defunt_pere = models.ForeignKey(Person, related_name=u'Père Défunt(e)', verbose_name=u'Père Défunt(e)', null=False)
    fk_defunt_mere = models.ForeignKey(Person, related_name=u'Mère Défunt(e)', verbose_name=u'Mère Défunt(e)', null=False)

    fk_declarant = models.ForeignKey(Person, related_name=u'Déclarant(e)', verbose_name=u'Déclarant(e)', null=False)
    parente = models.CharField(choices=PARENTE_CHOICES, verbose_name=u'Lien de parenté')
    parente_autre = models.CharField(null=True, max_length=30, verbose_name='Autre')

    date_deces = models.BooleanField(null=False, verbose_name=u"Date décès connue")
    heure_deces = models.BooleanField(null=False, verbose_name=u"Heure décès connue")
    heure_deces_detail = models.TimeField(null=True, verbose_name=u'Heure de décès')
    lieu_deces = models.CharField(choices=LIEU_CHOICES, verbose_name=u'Lieu du décès')
    adress_deces = models.CharField(max_length=30, verbose_name=u'Adresse décès')
    city_deces = models.CharField(max_length=30, verbose_name=u'Ville décès')
    zip_deces = models.IntegerField(verbose_name=u'Code Postal décès')
    country_deces = CountryField(blank_label=u'Sélectionner un pays', verbose_name=u'Pays décès')

    mairie = models.CharField(max_length=30, null=False, verbose_name='Mairie', default=" ")
    created = models.DateTimeField(auto_now_add=True)
    utilisateur = models.CharField(max_length=100, null=False, verbose_name="utilisateur", default=" ")

来自models.py Django App的Person

#coding:utf-8

from django.db import models
from django.utils.encoding import force_text
from django_countries.fields import CountryField


######################################
# Choix à l'utilisateur pour le sexe #
######################################

SEX_CHOICES = (
    ('Masculin', 'Masculin'),
    (u'Féminin', u'Féminin')
)
##########################################
# Choix à l'utilisateur pour la civilité #
##########################################

TITLE_CHOICES = (
    ('M', 'Monsieur'),
    ('Mlle', 'Mademoiselle'),
    ('Mme','Madame'),
    ('Dr','Docteur'),
    ('Me',u'Maître'),
    ('X','X'),
)

STATUS_CHOICES = (
    (u'Célibataire', u'Célibataire'),
    (u'Marié(e)', u'Marié(e)'),
    (u'Divorcé(e)', u'Divorcé(e)'),
    ('Veuf/Veuve', 'Veuf/Veuve'),
    ('X','X'),
)

####################################################################################
# Création d'une table permettant de renseigner toutes les informations concernant #
#                les parents et reprise de celles des enfants                      #
####################################################################################

class Person(models.Model):

    social_number = models.CharField(max_length=30, null=True, verbose_name='numero social', unique=True)
    title = models.CharField(max_length=12,choices=TITLE_CHOICES, verbose_name='Civilité')
    young_girl_lastname = models.CharField(max_length=30, verbose_name='Nom de jeune fille', blank=True)
    lastname = models.CharField(max_length=30, verbose_name='Nom de famille')
    firstname = models.CharField(max_length=30, verbose_name='Prénom(s)')
    sex = models.CharField(max_length=30, choices=SEX_CHOICES, verbose_name='Sexe')
    status = models.CharField(max_length=30, choices=STATUS_CHOICES, verbose_name="Statut civil")
    birthday = models.DateField(verbose_name='Date de naissance')
    birthcity = models.CharField(max_length=30, verbose_name='Ville de naissance')
    birthcountry = CountryField(blank_label='Sélectionner un pays', verbose_name='Pays de naissance')
    birthmairie = models.CharField(max_length=30, verbose_name='Mairie de naissance')
    nationality1 = models.CharField(max_length=30, verbose_name='Nationalité 1')
    nationality2 = models.CharField(max_length=30, verbose_name='Nationalité 2', null=True, blank=True)
    job = models.CharField(max_length=30, verbose_name='Profession')
    adress = models.CharField(max_length=30, verbose_name='Adresse')
    city = models.CharField(max_length=30, verbose_name='Ville')
    zip = models.IntegerField(verbose_name='Code Postal')
    country = CountryField(blank_label='Sélectionner un pays', verbose_name='Pays')
    mail = models.CharField(max_length=30, verbose_name='Email', blank=True)
    phone = models.CharField(max_length=20, verbose_name='Téléphone', blank=True)
    created = models.DateTimeField(auto_now_add=True)
    mairie = models.CharField(max_length=30, null=False, verbose_name='Mairie', default=' ')
    utilisateur = models.CharField(max_length=100, null=False, verbose_name="utilisateur", default=" ")
    vie = models.BooleanField(default=" ", null=False, verbose_name="En vie")

    def save(self, *args, **kwargs):
        for field_name in ['young_girl_lastname' ,'lastname', 'birthcity', 'nationality1', 'nationality2', 'city', 'birthmairie']:
            val = getattr(self, field_name, False)
            if val:
                setattr(self, field_name, val.upper())

        for field_name in ['firstname']:
            val = getattr(self, field_name, False)
            if val:
                new_val = []
                words = val.split()
                for x in words:
                    x = x.capitalize()
                    new_val.append(x)
                val = " ".join(new_val)
                setattr(self, field_name, val.capitalize())

        super(Person, self).save(*args, **kwargs)


    def __unicode__(self):
        return '%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s' % (self.id, self.title, self.young_girl_lastname ,self.lastname, self.status, self.firstname, self.sex, self.birthday, self.birthcity, self.birthcountry,
                                                                             self.birthmairie, self.nationality1, self.nationality2, self.job, self.adress, self.city, self.zip, self.country, self.mail, self.phone, self.vie)

你有什么想法吗?

0 个答案:

没有答案