我在用Django中文用户名查询时遇到错误:“用户不存在”。 相关代码:
reload(sys)
sys.setdefaultencoding('utf-8')
user_name = sheet.cell_value(r, 7).replace(' ', '')
equipment.eq_receiver = User.objects.get(user_name=user_name)
class User(AbstractUser):
user_id = models.AutoField(primary_key=True)
user_name = models.CharField(max_length=20)
回溯:
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\django\core\handlers\exception.py", line 41, in inner
response = get_response(request)
File "C:\Python27\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Python27\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Python27\lib\site-packages\django\contrib\auth\decorators.py", line 23, in _wrapped_view
return view_func(request, *args, **kwargs)
File "F:/repositories/ITIMS\eqp_mgt\views.py", line 652, in import_excel
equipment.eq_receiver = User.objects.get(user_name=user_name)
File "C:\Python27\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Python27\lib\site-packages\django\db\models\query.py", line 380, in get
self.model._meta.object_name
DoesNotExist: User matching query does not exist.
在Debugger中,我得到了 user_name 的值:
user_name={unicode}u"邵阳"
然后在Pycharm的 Watches 视图中,我尝试了其他一些查询,但结果让我困惑:
User.objects.get(user_name=u'\u90b5\u9633')={User}邵阳 #unicode of '邵阳'
User.objects.get(user_name=u'邵阳')={DoesNotExist}User matching query does not exist.
User.objects.get(user_name='邵阳')={User}邵阳
User.objects.get(user_name=user_name)={DoesNotExist}User matching query does not exist.