Python flask棉花糖-如何有条件地生成HATEOAS URL

Python flask棉花糖-如何有条件地生成HATEOAS URL,python,flask,Python,Flask,我希望根据对象的当前状态有选择地返回一些URL,并且在解决如何在模式中公开state属性、执行一些逻辑以及根据对象状态确定要返回哪些URL方面花费了大量时间: 模型: class Car(Model): model = Column(String) year = Column(String) running = Column(Boolean) #'0 = not running', '1 = running' 以及模式: class CarSchema(ma.Sch

我希望根据对象的当前状态有选择地返回一些URL,并且在解决如何在模式中公开state属性、执行一些逻辑以及根据对象状态确定要返回哪些URL方面花费了大量时间:

模型:

class Car(Model):
    model = Column(String)
    year = Column(String)
    running = Column(Boolean)   #'0 = not running', '1 = running'
以及模式:

class CarSchema(ma.Schema):
    class Meta:
        fields = ('model', 'year', 'running',  '_links')

    _links = ma.Hyperlinks({
        'self': ma.URLFor('car_detail', id='<id>'),
        'start': ma.URLFor('car_start', id='<id>')
        'stop': ma.URLFor('car_start', id='<id>')
    })
class-CarSchema(ma.Schema):
类元:
字段=('model'、'year'、'running'、'u links')
_links=ma.超链接({
'self':ma.URLFor('car_detail',id=''),
'start':ma.URLFor('car\u start',id='')
'stop':ma.URLFor('car\u start',id='')
})
我想做的是只有当“running”属性为0时才返回start url,当它为1时才返回stop url,但我不清楚如何实现这一点


棉花糖似乎有一些装饰,似乎会,但我该如何利用他们的烧瓶棉花糖

您可以使用post_dump post-processing完成:检查运行状态并删除不适当的字段。这将比有条件地生成它们容易得多

class CarSchema(ma.Schema):
    class Meta:
        fields = ('model', 'year', 'running', '_links')

    _links = ma.Hyperlinks({
        'self': ma.URLFor('car_detail', id='<id>'),
        'start': ma.URLFor('car_start', id='<id>')
        'stop': ma.URLFor('car_start', id='<id>')
    })

    @ma.post_dump
    def postprocess(self, data):
        if data['running']:
            del data['_links']['start']
        else:
            del data['_links']['stop']
class-CarSchema(ma.Schema):
类元:
字段=('model'、'year'、'running'、'u links')
_links=ma.超链接({
'self':ma.URLFor('car_detail',id=''),
'start':ma.URLFor('car\u start',id='')
'stop':ma.URLFor('car\u start',id='')
})
@马·波斯顿
def后处理(自身、数据):
如果数据[‘运行’]:
删除数据[''u链接]['start']
其他:
删除数据[''u链接]['stop']