python - TypeError: Decimal('0.00') 在石墨烯突变对象响应上不是 JSON 可序列化的
问题描述
该应用程序建立在Flask
我目前正在开发用户重置密码功能的基础之上。这被定义为客户端发布请求的突变。
客户端将提供其验证密码,然后向他发送一封电子邮件,突变将返回一个UserField
标量类型。
此外,我正在使用marshmallow
库来序列化数据。
模型.py
class User(db.model):
__tablename__ = 'users'
name = db.Column(db.Boolean)
email = db.Column(db.Unicode, unique=True, nullable=False)
credit_account = db.Column(db.Numeric(precision=12, scale=2), default=0)
序列化程序.py
class UserSerializer(Schema):
name = fields.String()
email = fields.String()
credit_account = fields.Decimal() # Decimal types here!!!
注意:UserSerializer 上有一个 Decimal 字段,这是 graphql 上的根本问题
下面的代码是GraphQL
Mutation 的实现。我们使用graphene
python库。
字段.py
class UserField(Scalar):
@staticmethod
def serialize(user):
json = UserSerializer()
return json.dump(user).data
@staticmethod
def parse_literal(node):
pass
@staticmethod
def parse_value(value):
pass
突变.py
class ResetUserPassword(graphene.Mutation):
user = graphene.Field(UserField)
def mutate(self, info, password):
user = get_user(password=password)
send_email(user=user,email=user.email, template='reset_password_template')
return ResetUserPassword(user=user)
用法:
{
"query": "mutation { resetPassword(password: \"foobar\") { user }}"
}
现在,我在控制台上收到了这个 TypeError :
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: Decimal('0.00') is not JSON serializable
解决方案
推荐阅读
- flutter - 如何复制具有新日期的旧文档(颤振 Firestore)?
- python-3.x - 在 pysimplegui 中的弹出窗口上单击确定后 GUI 挂起
- google-apps-script - Google Docs 脚本查找和替换数组问题
- mysql - 使用 node-mysql 的 Node 应用程序的 Nginx 504 超时
- mysql - 如何使用 node.js 插入多个 mysql 查询?
- python - 如何使用py-substrate-interface获取Polkadot中所有账户的账户数据
- assembly - 谁将进程的数据加载到数据路径中的寄存器库中?
- node.js - 请求正文用空格替换加号
- salesforce - ContentVersion 上的 Salesforce DataLoader 错误
- javascript - 如何更改快速 GET 响应中的字段名称?