我有这样的混合财产
@hybrid_property
def address(self):
return ', '.join([
self.address_street,
self.address_city,
self.address_state,
self.address_zip
])
@address.setter
def address(self, address):
g = geocoder.google(address)
if g.street_number and g.street_name:
self.address_street = g.street_number + ' ' + g.street_name
if g.city:
self.address_city = g.city
if g.state:
self.address_state = g.state
if g.postal:
self.address_zip = g.postal
if g.latlng:
self.address_lat = g.lat
self.address_lng = g.lng
@address.expression
def address(cls):
return cls.address_street + ', ' + cls.address_city + ', ' + cls.address_state + ', ' + cls.address_zip
使用基本的 Flask-Restless API...
manager = APIManager(app, flask_sqlalchemy_db=db)
member_bp = manager.create_api(
Member,
collection_name='members',
methods=['GET', 'POST', 'PATCH'],
include_columns=[
'id',
'first_name',
'last_name',
'email',
'created_at',
'signup_code',
]
)
但是 Flask-Restless 给了我一个 400 并说Model does not have field 'address'
。我还设置了另一个 hybrid_property,但它并没有抱怨。我无法弄清楚这个 hybrid_property 有什么问题。
奇怪的是,我可以通过include_columns
参数在我的 API 中公开属性。