我有像这样的混合属性
@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中公开属性。