首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >具有PhoneNumber类型的django-phone字段对象的django-graphql-jwt不是JSON可序列化的

具有PhoneNumber类型的django-phone字段对象的django-graphql-jwt不是JSON可序列化的
EN

Stack Overflow用户
提问于 2021-03-26 03:51:49
回答 1查看 94关注 0票数 2

我目前也在使用graphene-django的项目中使用django-phone-field库。

这是我的自定义用户模型,其中我使用的是PhoneField。

代码语言:javascript
复制
class User(AbstractBaseUser, PermissionsMixin):
    """
    Custom User model to add the required phone number
    """
    first_name = models.CharField(
        max_length=100
    )
    last_name = models.CharField(
        max_length=100
    )
    email = models.EmailField(
        verbose_name='Email address',
        max_length=255,
        unique=True,
        # Because it's unique, blank is not an option because there could be 2 with the same value ('')
        blank=False,
        null=True
    )
    phone = CustomPhoneField(
        # Please note this formatting only works for US phone numbers right now
        # https://github.com/VeryApt/django-phone-field#usage
        # If international numbers are needed at some point, take a look at:
        # https://github.com/stefanfoulis/django-phonenumber-field
        E164_only=True,
        unique=True
    )
    is_active = models.BooleanField(default=True)
    is_admin = models.BooleanField(default=False)

    objects = UserManager()

    USERNAME_FIELD = 'phone'
    REQUIRED_FIELDS = ['first_name', 'last_name',]

我正在使用django-graphql-jwt来处理身份验证。

下面是我的tokenAuth查询:

代码语言:javascript
复制
mutation TokenAuth ($phone: String!, $password: String!) {
    tokenAuth(phone: $phone, password: $password) {
        token
        payload
        refreshExpiresIn
    }
}

服务器在尝试执行该查询时检索到错误:

代码语言:javascript
复制
{
    "errors": [
        {
            "message": "Object of type PhoneNumber is not JSON serializable",
            "locations": [
                {
                    "line": 2,
                    "column": 5
                }
            ],
            "path": [
                "tokenAuth"
            ]
        }
    ],
    "data": {
        "tokenAuth": null
    }
}

我临时做了一个猴子补丁,覆盖了PhoneField的to_python方法,如下所示:

代码语言:javascript
复制
class CustomPhoneField(PhoneField):
    """
    I had to override this to_python method because the phone field is now being used as the username field.
    In GraphQL JWT I was getting the following exception:
    Object of type PhoneNumber is not JSON serializable

    I don't really understand the implications of overriding this method.
    """
    def to_python(self, value):
        # Called during deserialization and from clean() methods in forms
        if not value:
            return None
        elif isinstance(value, PhoneNumber):
            return str(value)
        return str(value)

我想知道这个问题的正确解决方案是什么,也想了解它是如何工作的,这样我就可以提出一个更优雅的解决方案。

EN

回答 1

Stack Overflow用户

发布于 2021-05-20 19:22:00

看一下您的问题,我认为您正在尝试使用字符串而不是Json,如果phone number是可以尝试使用JsonString数据类型的JSON字段,则可以克服field.This。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66806638

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档