Emmanuel Mtali
Emmanuel Mtali

Reputation: 4973

TypeError: __str__ returned non-string (type PhoneNumber)

I am using PhoneNumberField in my django-rest-api application

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

i was successful creating superuser with phonenumber and password.But each time am running the server i get error TypeError: __str__ returned non-string (type PhoneNumber) Can someone please help me

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)

Upvotes: 4

Views: 5175

Answers (2)

Gedeon Mutshipayi
Gedeon Mutshipayi

Reputation: 4083

A django-phonenumber-field object is an instance of <class 'phonenumber_field.phonenumber.PhoneNumber'>, so you need to convert to a type string for django to present it.

There are several ways to do this, here is one of the ways.

Add the str method to your model:

def __str__(self):
    return f"{self.phone_number}"

Upvotes: 0

Evans Murithi
Evans Murithi

Reputation: 3257

From django-phonenumber-field it says:

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

You should do something like:

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

Upvotes: 14

Related Questions