咦,為啥我的模型不能使用嘞冰木?
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)
原因和模型的配置有關(guān)
在模型中颂砸,我們有一段默認(rèn)輸出的字段信息羡洁,注意看他定義的是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>]>