带有@pre_dump帮助程序的棉花糖序列化问题

时间:2019-03-23 14:02:52

标签: serialization flask-sqlalchemy marshmallow

我在棉花糖中具有以下架构

class JobSchema(ma.ModelSchema):



    title = fields.Str(required=True, validate=[validate.Length(min=4, max=80)])
    city = fields.Str(required=True, validate=[validate.Length(min=5, max=100)])
    state = fields.Str(required=True, validate=validate_state)
    zipCode = fields.Str(required=True, validate=validate_zipCode)
    description = fields.Str(required=False, validate=[validate.Length(max=250)])
    narrative = fields.Str(required=False, validate=[validate.Length(max=250)])
    companyLogo = fields.Str(required=False, validate=[validate.Length(max=250)])
    companyName = fields.Str(required=True, validate=[validate.Length(min=5, max=250)])
    companyURL = fields.Str(required=True, validate=[validate.Length(min=4, max=100)])
    latitude = fields.Str(required=True)
    longitude = fields.Str(required=True)
    time_created = fields.DateTime()
    active = fields.Boolean(required=True)

    @pre_dump(pass_many=True)
    def pre_dump_operations(self, data, many):
        import pdb; pdb.set_trace()

    @post_dump(pass_many=True)
    def post_dump_operations(self, data, many):
        import pdb; pdb.set_trace()

    class Meta:
        model = modelJob

I have a query that returns a list of Jobs plus a calculated value 

(距离)(,7.56971 ..)     因此,从查询返回的值位于@pre_dump SQLAlchemy帮助器的参数'data'中,如下所示     (在@pre_dump中调试)

    Pdb) data
   [(<Job 23>, 7.57971960007271), (<Job 25>, 8.00091287114993), (<Job 26>, 8.00091287114993), (<Job 24>, 9.19538792085964), (<Job 36>, 9.58169814221016)]

看看我得到的“自我”和“很多”(只是为了给您提供更多元素)

   (Pdb) self
     <JobSchema(many=True, strict=False)>
    (Pdb) many
     True

好吧,现在我想把距离加到Job中,这样它就可以作为Job的属性序列化到视图中了(请参阅序列化Object的结尾)

进一步了解我得到的数据

 (Pdb) data[0]
 (<Job 23>, 7.57971960007271)

但是看起来很简单:

Pdb) type(data[0])
 <class 'sqlalchemy.util._collections.result'>

所以让我们看看这是什么

(Pdb) dir(data[0])
 ['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__module__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__sizeof__', '__slots__', '__str__', '__subclasshook__', '_asdict', '_fields', '_real_fields', 'count', 'distance', 'index', 'keys']

继续挖掘:

 (Pdb) results = data[0]._asdict()
 (Pdb) results
 {'distance': 7.57971960007271}


    (    Pdb) data[0] 
    (<Job 23>, 7.57971960007271)

因此,尽管有上述所有详细信息,我还是无法理解如何序列化每个作业,并以距离作为作业的新字段。 例如,在下面的示例中,距离现在是作业的字段

23: {
        "latitude": "25.824725",
        "title": "Job Title 1",
        "description": "DS",
        "companyName": "PySS",
        "id": 3,
        "zipCode": "33878",
        "state": "NY",
        "longitude": "-80.30476",
        "companyURL": "http://www.cnn.com",
        "city": "Miami"
        "distance": 7.57971960007271
    },

这里有人可以理解并可以解释吗? 谢谢

0 个答案:

没有答案