当我在mod中添加ForeignKey关系时,“ascii”编解码器无法对位置01中的字符进行编码:序号不在范围(128)内

2024-04-25 06:25:19 发布

您现在位置:Python中文网/ 问答频道 /正文

很抱歉重复的问题,但任何问题都没有帮助我,所以我再次问它 我的模型.py在

class Country(models.Model):
    countryId=models.IntegerField(default=0)
    country=models.CharField(max_length=50)

    def __str__(self):
        return self.country

class State(models.Model):
    state_id=models.IntegerField(default=0)
    state=models.CharField(max_length=50)
    countryId=models.ForeignKey(Country,default='000')

    def __str__(self):
        return self.state


class City(models.Model):
    cityid=models.IntegerField(default=0)
    city=models.CharField(max_length=50)
    state_id=models.ForeignKey(State,default='000')

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

    def __str__(self):
        return ('Proposal for: %s' % self.city).encode('utf8')

当我在州里评论ForeignKey时,它工作得很好。 如果我使用这个ForeignKey,它会抛出一个错误'ascii'codec cannot encode at position 0-1:序号不在范围内(128)

^{pr2}$

回溯是

File "/home/cpverma/work/school/env/local/lib/python2.7/site-packages/django/utils/html.py" in <lambda>
  390.         klass.__unicode__ = lambda self: mark_safe(klass_unicode(self))
File "/home/cpverma/work/school/env/local/lib/python2.7/site-packages/django/forms/forms.py" in __str__
  537.         return self.as_widget()
File "/home/cpverma/work/school/env/local/lib/python2.7/site-packages/django/forms/forms.py" in as_widget
  593.         return force_text(widget.render(name, self.value(), attrs=attrs))
File "/home/cpverma/work/school/env/local/lib/python2.7/site-packages/django/contrib/admin/widgets.py" in render
  292.             'widget': self.widget.render(name, value, *args, **kwargs),
File "/home/cpverma/work/school/env/local/lib/python2.7/site-packages/django/forms/widgets.py" in render
  513.         options = self.render_options(choices, [value])
File "/home/cpverma/work/school/env/local/lib/python2.7/site-packages/django/forms/widgets.py" in render_options
  539.         for option_value, option_label in chain(self.choices, choices):
File "/home/cpverma/work/school/env/local/lib/python2.7/site-packages/django/forms/models.py" in __iter__
  1108.                 yield self.choice(obj)
File "/home/cpverma/work/school/env/local/lib/python2.7/site-packages/django/forms/models.py" in choice
  1115.         return (self.field.prepare_value(obj), self.field.label_from_instance(obj))
File "/home/cpverma/work/school/env/local/lib/python2.7/site-packages/django/forms/models.py" in label_from_instance
  1186.         return smart_text(obj)
File "/home/cpverma/work/school/env/local/lib/python2.7/site-packages/django/utils/encoding.py" in smart_text
  56.     return force_text(s, encoding, strings_only, errors)
File "/home/cpverma/work/school/env/local/lib/python2.7/site-packages/django/utils/encoding.py" in force_text
  94.                 s = six.text_type(bytes(s), encoding, errors)

Exception Type: UnicodeEncodeError at /admin/city/city/49840/
Exception Value: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)

Tags: djangoinpyselfenvhomemodelslib
2条回答

如果您使用Python2(看起来确实如此),那么应该使用__unicode__而不是{},例如:

def __unicode__(self):
    return self.state

这将解决您的编码问题。在

但是在python3中__str__的方式(因为普通字符串和unicode字符串之间的划分在python3中不存在)。在

我找到了解决办法。解决方法是:

return self.state.encode('utf8')

相关问题 更多 >