TypeError:__str__返回非字符串(类型PhoneNumber)

时间:2016-12-22 17:33:58

标签: python django django-rest-framework

我在我的django-rest-api应用程序中使用PhoneNumberField

class User(AbstractBaseUser, PermissionsMixin):
    phone_number = PhoneNumberField(_('phone number'), unique=True)

我成功使用phonenumber和密码创建超级用户。但每次运行服务器时我都会收到错误TypeError: __str__ returned non-string (type PhoneNumber)有人可以帮我吗

Stacktrace
TypeError: __str__ returned non-string (type PhoneNumber)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Emmanuel\Envs\myproject\lib\site-packages\django\core\handlers\exception.py", line 39, in inner
    response = get_response(request)
  File "C:\Users\Emmanuel\Envs\myproject\lib\site-packages\django\utils\deprecation.py", line 136, in __call__
    response = self.get_response(request)
  File "C:\Users\Emmanuel\Envs\myproject\lib\site-packages\django\core\handlers\exception.py", line 41, in inner
    response = response_for_exception(request, exc)
  File "C:\Users\Emmanuel\Envs\myproject\lib\site-packages\django\core\handlers\exception.py", line 86, in response_for_exception
    response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
  File "C:\Users\Emmanuel\Envs\myproject\lib\site-packages\django\core\handlers\exception.py", line 128, in handle_uncaught_exception
    return debug.technical_500_response(request, *exc_info)
  File "C:\Users\Emmanuel\Envs\myproject\lib\site-packages\django\views\debug.py", line 84, in technical_500_response
    html = reporter.get_traceback_html()
  File "C:\Users\Emmanuel\Envs\myproject\lib\site-packages\django\views\debug.py", line 317, in get_traceback_html
    return t.render(c)
  File "C:\Users\Emmanuel\Envs\myproject\lib\site-packages\django\template\base.py", line 208, in render
    return self._render(context)
  File "C:\Users\Emmanuel\Envs\myproject\lib\site-packages\django\template\base.py", line 199, in _render
    return self.nodelist.render(context)
  File "C:\Users\Emmanuel\Envs\myproject\lib\site-packages\django\template\base.py", line 994, in render
    bit = node.render_annotated(context)
  File "C:\Users\Emmanuel\Envs\myproject\lib\site-packages\django\template\base.py", line 961, in render_annotated
    return self.render(context)
  File "C:\Users\Emmanuel\Envs\myproject\lib\site-packages\django\template\defaulttags.py", line 315, in render
    return nodelist.render(context)
  File "C:\Users\Emmanuel\Envs\myproject\lib\site-packages\django\template\base.py", line 994, in render
    bit = node.render_annotated(context)
  File "C:\Users\Emmanuel\Envs\myproject\lib\site-packages\django\template\base.py", line 961, in render_annotated
    return self.render(context)
  File "C:\Users\Emmanuel\Envs\myproject\lib\site-packages\django\template\defaulttags.py", line 315, in render
    return nodelist.render(context)
  File "C:\Users\Emmanuel\Envs\myproject\lib\site-packages\django\template\base.py", line 994, in render
    bit = node.render_annotated(context)
  File "C:\Users\Emmanuel\Envs\myproject\lib\site-packages\django\template\base.py", line 961, in render_annotated
    return self.render(context)
  File "C:\Users\Emmanuel\Envs\myproject\lib\site-packages\django\template\base.py", line 1050, in render
    return render_value_in_context(output, context)
  File "C:\Users\Emmanuel\Envs\myproject\lib\site-packages\django\template\base.py", line 1028, in render_value_in_context
    value = force_text(value)
  File "C:\Users\Emmanuel\Envs\myproject\lib\site-packages\django\utils\encoding.py", line 76, in force_text
    s = six.text_type(s)

1 个答案:

答案 0 :(得分:7)

来自django-phonenumber-field它说:

  

返回的对象是PhoneNumber实例,而不是字符串。

您应该执行以下操作:

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