简体   繁体   English

烧瓶棉花糖去掉小数点

[英]flask-marshmallow dropping the decimal point

I am using flask-marshmallow and flask-sqlalchemy, and I want to serialise a data set that contains a Numeric field, all the values have decimal places ie 2.5 but when marshmallow dumps it to a json it cuts off the decimal place (changes 2.5 to 2) where am I going wrong?我正在使用烧瓶棉花糖和烧瓶 sqlalchemy,我想序列化一个包含数字字段的数据集,所有值都有小数位,即 2.5,但是当棉花糖将其转储到 json 时,它会切断小数位(更改 2.5 2)我哪里出错了?

ma.py映射文件

from flask_marshmallow import Marshmallow

ma = Marshmallow()

app.py应用程序

import ma
import db
from flask_restful import Api
.....
api = Api(app)

api.add_resource(LookupActivities, '/lookup_activity_by_type/<string:activity_type>')

if __name__ == "__main__":
    db.init_app(app)
    ma.init_app(app)
    app.run(port=5000, debug=True)

model/lookup.py模型/查找.py

from db import db

class LookupModel(db.Model):
    __tablename__ = "asmirt_cpd_point_lookup"
    __table_args__ = {"schema": "radcpd"}

    asmirt_activity_id = db.Column(db.Integer, primary_key=True)
    activity_type = db.Column(db.String(255))
    activity = db.Column(db.String(255))
    alt_text = db.Column(db.String(255))
    activity_points = db.Column(db.Integer)
    per_unit = db.Column(db.String(255))

    @classmethod
    def find_by_activity_type(cls, activity_type: str):
        return cls.query.filter_by(activity_type=activity_type).all()

schema/lookup.py架构/lookup.py

from ma import ma
from models.lookup import LookupModel


class LookupSchema(ma.ModelSchema):
    class Meta:
        model = LookupModel
        dump_only = ("asmirt_activity_id",)

resource/lookup.py资源/lookup.py

from flask_restful import Resource
from models.lookup import LookupModel
from schemas.lookup import LookupSchema
from flask_jwt_extended import jwt_required
from flask import jsonify

class LookupActivities(Resource):
    @classmethod
    def get(cls, activity_type):
        data = LookupModel.find_by_activity_type(activity_type=activity_type)
        print(data)
        print(data[1].activity)
        if data:
            return {"activities": lookup_schema_list.dump(data)}, 200

        return {'message', 'No activities in that group'}

For this example it will change the value 2.5 in the database but outputs 2. In other examples I will get the error message Object Decimal is not serializable.对于本示例,它将更改数据库中的值 2.5,但输出 2。在其他示例中,我将收到错误消息 Object Decimal is not serializable。 Anyone know what is causing this and how to fix it?任何人都知道是什么导致了这个以及如何解决它?

In your ma.ModelSchema class you need to override the decimal value like this:在您的ma.ModelSchema类中,您需要像这样覆盖十进制值:

from flask_marshmallow.fields import fields

class LookupSchema(ma.ModelSchema):
    per_unit = fields.Float()

    class Meta:
        model = LookupModel
        dump_only = ("asmirt_activity_id",)

Now you can serialize per_unit property.现在您可以序列化per_unit属性。 Repeat this for all your properties.对所有属性重复此操作。 You can use Decimal instead of Float .您可以使用Decimal而不是Float

Don't forget : in your db.model , per_unit has to be Float, like: per_unit = db.Column(db.Float())不要忘记:在您的db.modelper_unit必须是 Float,例如: per_unit = db.Column(db.Float())

Your model columns do not have decimal declaration. 您的模型列没有十进制声明。 I guess you're trying to insert / get decimal data to / from integer declared columns. 我猜您正在尝试从声明的整数列中插入十进制数据或从中获取十进制数据。 Try declaring the column as decimal. 尝试将列声明为十进制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM