python - 类型错误 : __str__ returned non-string (type PhoneNumber)

标签 python django django-rest-framework

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

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

我成功创建了带有电话号码和密码的 super 用户。但每次运行服务器时,我都会收到错误消息 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)

最佳答案

来自 django-phonenumber-field它说:

The object returned is a PhoneNumber instance, not a string.

你应该这样做:

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

关于python - 类型错误 : __str__ returned non-string (type PhoneNumber),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41288844/

相关文章:

python - 这是什么意思? xarray 错误 : cannot handle a non-unique multi-index

python - 为什么python在 "."之后允许对象和方法名之间有空格

python - Django 类型错误 : __init__() takes 1 positional argument but 2 were given

python - sqlalchemy 中的 scoped_session(sessionmaker()) 还是普通的 sessionmaker()?

python - Django 管理命令 : provide base class without circular reference

python - 将不在模型中的字段添加到 Django REST 框架中的序列化程序

python - 当另一列的列表包含特定值时返回 pandas df 的列

python - 将 JSON 数组嵌套到 Python Pandas DataFrame

python - 如何获取外键对象以在 django rest 框架中显示完整对象

python - 使用 Django Rest Framework 订购带有排序查询参数的detail_route