这可以通过创建自定义序列化程序和视图来实现。
序列化程序.py
# Import django packages
from django.utils.translation import gettext_lazy as _
# Import external packages
from rest_framework_simplejwt.serializers import TokenObtainPairSerializer as SimpleTokenObtainPairSerializer
class TokenObtainPairSerializer(SimpleTokenObtainPairSerializer):
default_error_messages = {
'no_active_account': _('CUSTOM ERROR MESSAGE HERE')
}
视图.py
# Import external packages
from rest_framework_simplejwt.views import TokenObtainPairView as SimpleTokenObtainPairView
# Import my packages
from gadget.auth.serializers import TokenObtainPairSerializer
class TokenObtainPairView(SimpleTokenObtainPairView):
serializer_class = TokenObtainPairSerializer
最后,更新您的网址以使用您的新视图。
网址.py
# Import external packages
from rest_framework_simplejwt.views import TokenRefreshView
# Import my packages
from .views import TokenObtainPairView
urlpatterns = [
# Token authentication
path(r'token/', TokenObtainPairView.as_view(), name='token_obtain_pair'),
path(r'token/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
]