如何限制在 Flask-RestPlus 中使用 fields.list 返回的内容
How to limit what gets returned with fields.list in Flask-RestPlus
每个建筑物都有单元,每个单元都与一个帐户相关联。一个单元可以关联多个帐户。每个单位只能有一个状态为 "active".
的帐户
API return 是所有单位的列表,嵌套在每个单位中的是 所有 与该单位关联的帐户的列表。
如何 return 相同的单位列表,但只有 活动 帐户与每个单位相关联?
#model.py
class Unit(db.Model):
__tablename__ = 'units'
...
building_id = db.Column(db.Integer, db.ForeignKey('buildings.id'))
building = db.relationship("Building", back_populates="units")
accounts = db.relationship("Account", back_populates="unit")
class Account(db.Model):
__tablename__ = 'accounts'
id = db.Column(db.Integer, primary_key=True)
status = db.Column(sqlalchemy_utils.ChoiceType(STATUS_CHOICES))
...
#building.py
@api.route('/units')
class BuildingUnits(Resource):
@api.marshal_with(schemas.unit_fields, envelope='data')
def get(self):
""" Get list of all units
"""
return rbac.current_identity.building.units
#schemas.py
unit_fields = api.model('unit_fields', {
'id': fields.String,
...etc
...etc
'accounts': fields.List(fields.Nested(account_fields)),
})
account_fields = api.model('account_fields', {
'id': fields.String,
...etc
...etc
'status': fields.String(attribute=lambda x: getattr(x.status, 'value', 'Inactive')),
})
问了一个人(我不确定谁想让我宣传是谁)
他回复如下:
This is actually a good example why I tend to prefer to not use
extensions to handle the API payloads. Sooner or later you find
something that you need isn’t directly supported and you end up having
to create compromise solutions.
What I would do in your case, is define a @property in your model that
is maybe called active_account. This would be implemented as a
database query that returns that one account that you want singled
out. Then you can add “active_account” to your schema, so that
Flask-RESTful renders it as a related item.
Hope this helps.
我认为这是一个很好的答案,它解决了我的问题。
然后我跟进了:
Im not quite sure what you're referring to when you said: "why I tend
to prefer to not use extensions to handle the API payloads."
Which extension are you referring to. What exactly would you do to
implement what I have without using "extensions"
他回复:
You are using Flask-RESTful, that is the extension I was referring to.
I really have nothing against Flask-RESTful, works well, but I prefer
to write my APIs in straight Flask.
这就是所有人,也许稍后我会 post 我是如何实现他所说的。
每个建筑物都有单元,每个单元都与一个帐户相关联。一个单元可以关联多个帐户。每个单位只能有一个状态为 "active".
的帐户API return 是所有单位的列表,嵌套在每个单位中的是 所有 与该单位关联的帐户的列表。
如何 return 相同的单位列表,但只有 活动 帐户与每个单位相关联?
#model.py
class Unit(db.Model):
__tablename__ = 'units'
...
building_id = db.Column(db.Integer, db.ForeignKey('buildings.id'))
building = db.relationship("Building", back_populates="units")
accounts = db.relationship("Account", back_populates="unit")
class Account(db.Model):
__tablename__ = 'accounts'
id = db.Column(db.Integer, primary_key=True)
status = db.Column(sqlalchemy_utils.ChoiceType(STATUS_CHOICES))
...
#building.py
@api.route('/units')
class BuildingUnits(Resource):
@api.marshal_with(schemas.unit_fields, envelope='data')
def get(self):
""" Get list of all units
"""
return rbac.current_identity.building.units
#schemas.py
unit_fields = api.model('unit_fields', {
'id': fields.String,
...etc
...etc
'accounts': fields.List(fields.Nested(account_fields)),
})
account_fields = api.model('account_fields', {
'id': fields.String,
...etc
...etc
'status': fields.String(attribute=lambda x: getattr(x.status, 'value', 'Inactive')),
})
问了一个人(我不确定谁想让我宣传是谁)
他回复如下:
This is actually a good example why I tend to prefer to not use extensions to handle the API payloads. Sooner or later you find something that you need isn’t directly supported and you end up having to create compromise solutions.
What I would do in your case, is define a @property in your model that is maybe called active_account. This would be implemented as a database query that returns that one account that you want singled out. Then you can add “active_account” to your schema, so that Flask-RESTful renders it as a related item.
Hope this helps.
我认为这是一个很好的答案,它解决了我的问题。
然后我跟进了:
Im not quite sure what you're referring to when you said: "why I tend to prefer to not use extensions to handle the API payloads."
Which extension are you referring to. What exactly would you do to implement what I have without using "extensions"
他回复:
You are using Flask-RESTful, that is the extension I was referring to. I really have nothing against Flask-RESTful, works well, but I prefer to write my APIs in straight Flask.
这就是所有人,也许稍后我会 post 我是如何实现他所说的。