【发布时间】:2018-05-14 19:18:06
【问题描述】:
:)
我开发了一个带有膳食价格计算功能的 Android Cook-App。
我几乎完成了我的 Api,但现在我得到一个 TypeError: 'RelationshipProperty' object is not iterable。
我的 sum(mealprice) 我有我的 json,但我喜欢用
查询我的 mealprice@classmethod
def find_by_mealprice(cls, mealprice):
return cls.query.filter_by(mealprice=mealprice).first()
但我只能在 json 方法中建立我的总和。
类 MealModel(db.Model):
__tablename__ = 'meal'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80))
description = db.Column(db.String)
usp = db.Column(db.String)
workTime = db.Column(db.TIME)
mainIngredients =db.Column(db.String)
img = db.Column(db.String)
difficulty_id = db.Column(db.Integer, db.ForeignKey('difficulty.id'))
difficulty = db.relationship('DifficultyModel')
diet_id = db.Column(db.Integer, db.ForeignKey('diet.id'))
diet = db.relationship('DietModel')
category_id = db.Column(db.Integer, db.ForeignKey('category.id'))
category = db.relationship('CategoryModel')
recipes = db.relationship('RecipeModel', lazy="dynamic")
mealprice = (sum([(recipe.ingredients.price/recipe.ingredients.minamount * recipe.quantity) for recipe in recipes])) <-- TypeError?
def __repr__(self):
return (self.name)
def json(self):
mealprice = (sum([(recipe.ingredients.price/recipe.ingredients.minamount * recipe.quantity) for recipe in self.recipes]))
return { 'id': self.id,
'name': self.name,
'mIng': self.mainIngredients,
'usp': self.usp,
'difficulty': self.difficulty.name,
'workTime': self.workTime.isoformat(),
'diet': self.diet.name,
'description': self.description,
'mealImage': self.img,
'category': self.category.name,
'recipes': [recipe.json() for recipe in self.recipes],
'meal_price': mealprice
}
我希望这个问题不是愚蠢的,我是 Flask Api 和 Python 的新手,几个月前我开始使用 Android Studio 编程。
希望你能帮上忙! :) 怎么查询mealprice??
【问题讨论】:
标签: python flask flask-sqlalchemy