美文网首页
模型使用抛except (UnicodeEncodeError,

模型使用抛except (UnicodeEncodeError,

作者: 鸟它鸟 | 来源:发表于2019-08-15 11:08 被阅读0次

咦,为啥我的模型不能使用嘞?

In [1]: from hwcheck.models import CheckHardwareStatus

In [2]: CheckHardwareStatus.objects.all()
Out[2]: ---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/home/python36/lib/python3.6/site-packages/IPython/core/formatters.py in __call__(self, obj)
    700                 type_pprinters=self.type_printers,
    701                 deferred_pprinters=self.deferred_printers)
--> 702             printer.pretty(obj)
    703             printer.flush()
    704             return stream.getvalue()

/home/python36/lib/python3.6/site-packages/IPython/lib/pretty.py in pretty(self, obj)
    393                             if callable(meth):
    394                                 return meth(obj, self, cycle)
--> 395             return _default_pprint(obj, self, cycle)
    396         finally:
    397             self.end_group()

/home/python36/lib/python3.6/site-packages/IPython/lib/pretty.py in _default_pprint(obj, p, cycle)
    508     if _safe_getattr(klass, '__repr__', None) is not object.__repr__:
    509         # A user-provided repr. Find newlines and replace them with p.break_()
--> 510         _repr_pprint(obj, p, cycle)
    511         return
    512     p.begin_group(1, '<')

/home/python36/lib/python3.6/site-packages/IPython/lib/pretty.py in _repr_pprint(obj, p, cycle)
    699     """A pprint that just redirects to the normal repr function."""
    700     # Find newlines and replace them with p.break_()
--> 701     output = repr(obj)
    702     for idx,output_line in enumerate(output.splitlines()):
    703         if idx:

/home/python36/lib/python3.6/site-packages/django/db/models/query.py in __repr__(self)
    227         if len(data) > REPR_OUTPUT_SIZE:
    228             data[-1] = "...(remaining elements truncated)..."
--> 229         return '<%s %r>' % (self.__class__.__name__, data)
    230
    231     def __len__(self):

/home/python36/lib/python3.6/site-packages/django/db/models/base.py in __repr__(self)
    588     def __repr__(self):
    589         try:
--> 590             u = six.text_type(self)
    591         except (UnicodeEncodeError, UnicodeDecodeError):
    592             u = '[Bad Unicode data]'

TypeError: __str__ returned non-string (type int)

原因和模型的配置有关

在模型中,我们有一段默认输出的字段信息,注意看他定义的是str类型,若我们返回的是飞str类型,就会抛上边的异常。

    def __str__(self):
        return self.id

修改一下

    def __str__(self):
        return str(self.id)

然后再试试

In [2]: CheckHardwareStatus.objects.all()
Out[2]: <QuerySet [<CheckHardwareStatus: 20>, <CheckHardwareStatus: 21>, <CheckHardwareStatus: 22>, <CheckHardwareStatus: 23>, <CheckHardwareStatus: 24>, <CheckHardwareStatus: 25>, <CheckHardwareStatus: 26>, <CheckHardwareStatus: 27>, <CheckHardwareStatus: 28>, <CheckHardwareStatus: 29>, <CheckHardwareStatus: 30>, <CheckHardwareStatus: 31>]>

相关文章

网友评论

      本文标题:模型使用抛except (UnicodeEncodeError,

      本文链接:https://www.haomeiwen.com/subject/ejwajctx.html