简体   繁体   English

当 Django 查询集转换为列表时,表情符号的 mySQL Unicode 解码错误

[英]mySQL unicode decode error for emojis when the Django queryset is converted to a list

I am on Django 1.11, python 3.6 and mysqlclient 1.4.6.我在 Django 1.11、python 3.6 和 mysqlclient 1.4.6 上。 I need to convert a Django queryset into a list.我需要将 Django 查询集转换为列表。 The objects in the queryset have a field with a unicode emoji value.查询集中的对象有一个带有 unicode 表情符号值的字段。 (see: field=u'✅ This is the field value' ) That particular table and field in mySQL are using utf8mb4_unicode_ci encoding. (请参阅: field=u'✅ This is the field value' )mySQL 中的特定表和字段使用utf8mb4_unicode_ci编码。

When I run qs_list = list(qs) , MySQL throws a Unicode error which I think is caused by the emoji in the field when the queryset is evaluated.当我运行qs_list = list(qs) ,MySQL 会抛出一个 Unicode 错误,我认为这是由评估查询集时字段中的表情符号引起的。

File encoding:文件编码:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

Queryset:查询集:

qs = Fake.objects.filter(..)
qs_list = list(qs)

Error:错误:

<class 'UnicodeDecodeError'>
Exception: 'utf-8' codec can't decode byte 0xed in position 11: invalid continuation byte

File "/home/mysite/lib/python3.6/site-packages/django/db/models/query.py", line 250, in __iter__
self._fetch_all()
File "/home/mysite/lib/python3.6/site-packages/django/db/models/query.py", line 1121, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/home/mysite/lib/python3.6/site-packages/django/db/models/query.py", line 53, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch)
File "/home/mysite/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 899, in execute_sql
raise original_exception
File "/home/mysite/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 889, in execute_sql
cursor.execute(sql, params)
File "/home/mysite/lib/python3.6/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/home/mysite/lib/python3.6/site-packages/django/db/backends/mysql/base.py", line 101, in execute
return self.cursor.execute(query, args)
File "/home/mysite/lib/python3.6/site-packages/MySQLdb/cursors.py", line 209, in execute
res = self._query(query)
File "/home/mysite/lib/python3.6/site-packages/MySQLdb/cursors.py", line 317, in _query
self._post_get_result()
File "/home/mysite/lib/python3.6/site-packages/MySQLdb/cursors.py", line 352, in _post_get_result
self._rows = self._fetch_row(0)
File "/home/mysite/lib/python3.6/site-packages/MySQLdb/cursors.py", line 325, in _fetch_row
return self._result.fetch_row(size, self._fetch_type)

Is there a simple solution or a workaround for this?是否有一个简单的解决方案或解决方法? How can I get the list of objects in the queryset without queryset being evaluated?如何在不评估查询集的情况下获取查询集中的对象列表?

Most probably this is related to the encoding and you need to check other types of encoding as suggested in this article这很可能与编码有关,您需要按照本文中的建议检查其他类型的编码

Regarding the list of objects - try iterating on the result to get(for example) each name and store the values in a list:关于对象列表 - 尝试迭代结果以获取(例如)每个名称并将值存储在列表中:

qs = Fake.objects.filter(..)
qs_list = [x.name for x in qs]

where 'name' is the model field you need to iterate.其中“名称”是您需要迭代的模型字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM