Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django UnicodeCodeError:Can';无法将unicode字符串保存到数据库_Django_Python 2.7 - Fatal编程技术网

Django UnicodeCodeError:Can';无法将unicode字符串保存到数据库

Django UnicodeCodeError:Can';无法将unicode字符串保存到数据库,django,python-2.7,Django,Python 2.7,我有一个通过lxml从xml派生的unicode字符串: >>> name u'\u0414\u0438\u0434\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u0441\u0442\u043e\u043b \u0438\u0437 \u043c\u044f\u0433\u043a\u0438\u0445 \u0431\u043b\u043e\u043a\u043e\u0432.' 当我试图将其保

我有一个通过lxml从xml派生的unicode字符串:

>>> name
u'\u0414\u0438\u0434\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u0441\u0442\u043e\u043b \u0438\u0437 \u043c\u044f\u0433\u043a\u0438\u0445 \u0431\u043b\u043e\u043a\u043e\u0432.'
当我试图将其保存为模型属性之一的值时,我得到一个错误

product = Product(name=name, slug='er', category=c, description='wer', price=1)

UnicodeDecodeError: 'ascii' codec can't decode byte 0xd0 in position 12: ordinal not in range(128)
完全回溯:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/oleshko/design/detsad-komplekt/project/catalog/tools.py", line 31, in parseCML
    product = Product(name=name, slug='er', category=c, description='wer', price=1)
  File "/home/oleshko/design/.virtualenvs/detsad-komplekt/local/lib/python2.7/site-packages/django/db/models/base.py", line 468, in __init__
    setattr(self, field.name, rel_obj)
  File "/home/oleshko/design/.virtualenvs/detsad-komplekt/local/lib/python2.7/site-packages/django/db/models/fields/related.py", line 627, in __set__
    self.field.rel.to._meta.object_name,
UnicodeDecodeError: 'ascii' codec can't decode byte 0xd0 in position 12: ordinal not in range(128)
产品型号定义:

@python_2_unicode_compatible
class Product(TimeStampedModel):
    """ Product base class """
    sku = models.CharField(verbose_name='Артикул', max_length=50, blank=True, null=True)
    name = models.CharField( verbose_name='Название', max_length = 254 )
    slug = models.SlugField( max_length = 50, unique = True, help_text = 'название для URL' )
    category = models.ForeignKey(Category, verbose_name='Категория продукта', related_name='products')
    producer = models.ForeignKey(Producer, verbose_name='Бренд', related_name='producer_products', blank=True, null=True)
    description = RichTextField( verbose_name='Описание продукта', blank=True)
    collection = models.CharField(db_index=True, max_length=100, blank=True, verbose_name='Коллекция')
    price = models.DecimalField(verbose_name='Цена', max_digits=8, decimal_places=2)


    objects = InheritanceManager()

    def __str__(self):
        return self.name

您正试图将无效值设置为
产品类别
字段。它应该是实例,而不是查询集。尝试使用
first()
方法获取
类别
实例:

c = Category.objects.filter(integration_id=cat.text).first()
...
product = Product(name=name, slug='er', category=c, description='wer', price=1)

我怀疑当django为无效字段值生成错误消息时,会发生unicode错误。

异常的完整回溯是什么?如果没有更多的上下文,异常就没有意义。特别是,由于它已经是
unicode
对象,因此不会对其进行解码。该字符串中也没有
\xd0
代码点。UTF-8编码可能会,但Django本身不会尝试将编码与其他东西混合。你能分享
产品
模型定义吗?如果你使用的是python 2.7,请在主要帖子中添加一些注释,尝试用def _u-unicode __; self替换def u-str _;!谢谢!
c = Category.objects.filter(integration_id=cat.text).first()
...
product = Product(name=name, slug='er', category=c, description='wer', price=1)