我已经使用django-phonenumber-field作为我的电话号码模型字段和序列化程序。它工作正常,一切都很好。现在,我试图在这两个软件包djsoer和djangorestframework-jwt的Django Rest Framework中实现Json Web令牌。我正在管理旧的(django 1.11)代码库并将其迁移到Django2。现在,每当我尝试创建新的身份验证令牌时,都会收到内部服务器错误。
错误信息:
TypeError: Object of type 'PhoneNumber' is not JSON serializable
我知道Django-phonenumber-field包中的PhoneNumber对象对此负责。但是,错误堆栈在djangorestframework-jwt中。如何在JWT上添加/传递PhoneNumber序列化器?
[我没有得到哪个代码库是必需的,因此我没有添加任何代码库。问我,我稍后再描述/添加。]
TIA。
编辑#1:
追溯
Internal Server Error: /api/auth/create_token/
Traceback (most recent call last):
File "/home/a_o/.local/share/virtualenvs/conveyanceappbackend-Y2Y7Ynbu/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/home/a_o/.local/share/virtualenvs/conveyanceappbackend-Y2Y7Ynbu/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/a_o/.local/share/virtualenvs/conveyanceappbackend-Y2Y7Ynbu/lib/python3.6/site-packages/django/core/handlers/base.py", line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/a_o/.local/share/virtualenvs/conveyanceappbackend-Y2Y7Ynbu/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/home/a_o/.local/share/virtualenvs/conveyanceappbackend-Y2Y7Ynbu/lib/python3.6/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/home/a_o/.local/share/virtualenvs/conveyanceappbackend-Y2Y7Ynbu/lib/python3.6/site-packages/rest_framework/views.py", line 483, in dispatch
response = self.handle_exception(exc)
File "/home/a_o/.local/share/virtualenvs/conveyanceappbackend-Y2Y7Ynbu/lib/python3.6/site-packages/rest_framework/views.py", line 443, in handle_exception
self.raise_uncaught_exception(exc)
File "/home/a_o/.local/share/virtualenvs/conveyanceappbackend-Y2Y7Ynbu/lib/python3.6/site-packages/rest_framework/views.py", line 480, in dispatch
response = handler(request, *args, **kwargs)
File "/home/a_o/.local/share/virtualenvs/conveyanceappbackend-Y2Y7Ynbu/lib/python3.6/site-packages/rest_framework_jwt/views.py", line 57, in post
if serializer.is_valid():
File "/home/a_o/.local/share/virtualenvs/conveyanceappbackend-Y2Y7Ynbu/lib/python3.6/site-packages/rest_framework/serializers.py", line 236, in is_valid
self._validated_data = self.run_validation(self.initial_data)
File "/home/a_o/.local/share/virtualenvs/conveyanceappbackend-Y2Y7Ynbu/lib/python3.6/site-packages/rest_framework/serializers.py", line 437, in run_validation
value = self.validate(value)
File "/home/a_o/.local/share/virtualenvs/conveyanceappbackend-Y2Y7Ynbu/lib/python3.6/site-packages/rest_framework_jwt/serializers.py", line 60, in validate
'token': jwt_encode_handler(payload),
File "/home/a_o/.local/share/virtualenvs/conveyanceappbackend-Y2Y7Ynbu/lib/python3.6/site-packages/rest_framework_jwt/utils.py", line 95, in jwt_encode_handler
api_settings.JWT_ALGORITHM
File "/home/a_o/.local/share/virtualenvs/conveyanceappbackend-Y2Y7Ynbu/lib/python3.6/site-packages/jwt/api_jwt.py", line 62, in encode
cls=json_encoder
File "/usr/lib/python3.6/json/__init__.py", line 238, in dumps
**kw).encode(obj)
File "/usr/lib/python3.6/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib/python3.6/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "/usr/lib/python3.6/json/encoder.py", line 180, in default
o.__class__.__name__)
最佳答案
这与JWT不相关。 Rest Framework试图将“电话号码”字段序列化为json,但是它不知道该怎么做。查看有关如何实现a custom field的DRF文档-该字段负责了解如何在初始数据类型和序列化表示之间进行转换,反之亦然。
关于python - JWT如何与django-phonenumber-field一起使用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52868524/