gpt4 book ai didi

python - JWT 如何与 django-phonenumber-field 一起工作

转载 作者:太空宇宙 更新时间:2023-11-04 04:29:55 25 4
gpt4 key购买 nike

我用过django-phonenumber-field作为我的电话号码模型字段和序列化程序。它起作用了,一切都很好。现在,我正在尝试在这两个包的 Django Rest Framework 中实现 Json Web Token,djsoerdjangorestframework-jwt。我正在管理一个旧的 (django 1.11) 代码库并将其迁移到 Django2。现在,每当我尝试创建新的身份验证 token 时,都会收到内部服务器错误。错误信息:

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 正在尝试将 Phonenumber 字段序列化为 json,但它不知道该怎么做。查看 DRF 文档以了解如何实现 a custom field -- 该字段负责了解如何在初始数据类型和序列化表示之间进行转换,反之亦然。

关于python - JWT 如何与 django-phonenumber-field 一起工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52868524/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com