在搜索期间访问 Flask-Sqlalchemy 查询中的表变量
我正在使用 Flask 和 Flask-SQLalchemy 以及 Flask-Login 编写 RESTful api.我有一个模型 Users
初始化如下:
I am writing a RESTful api using Flask and Flask-SQLalchemy, as well as Flask-Login. I have a model Users
that's initialized like:
class Users(UserMixin, db.Model):
__tablename__ = "users"
# STATIC Standard required info
id = db.Column("id", db.Integer, primary_key = True)
public_id = db.Column("public_id", db.String(50), unique = True)
email = db.Column("email", db.String(50), unique = True)
username = db.Column("username", db.String(20), unique = True)
password = db.Column("password", db.String(20))
# DYNAMIC Standard required info
lat = db.Column("lat", db.Float)
lon = db.Column("lon", db.Float)
表中还有其他元素,但纬度和经度最为相关.我有一个方法 distanceMath()
,它接收当前登录用户的纬度和经度,以及表格中的纬度和经度来计算它们之间的距离.但我无法弄清楚如何在以下调用 distanceMath
的查询中访问表值而不会收到语法错误:
There are other elements in the table but the lat and lon are most relevant. I have a method distanceMath()
that takes in the currently logged in user's latitude and longitude, as well as a latitude and longitude from the table to calculate distance between them. But I can't figure out how to access the table values during the following query that calls distanceMath
without receiving a syntax error:
lat1 = current_user.lat
lon1 = current_user.lon
users = Users.query.filter(distanceMath(lat1, Users.lat, lon1, Users.lon)==1)
output = []
for user in users:
data = {}
data["username"] = user.username
output.append(data)
return str(output)
为了以防万一,这里是 distanceMath
方法:
Just in case, here's the distanceMath
method:
# Haversine Distance
def distanceMath(lat1, lat2, lon1, lon2):
distance = math.acos(math.sin(math.radians(lat1))*math.sin(math.radians(lat2))) + math.cos(math.radians(lat1))*math.cos(math.radians(lat2))*math.cos(math.radians(lon1)-math.radians(lon2))
return distance
错误的一个例子是:
TypeError: must be real number, not InstrumentedAttribute
根据我的理解,这实质上是说 User.lat
和 User.lon
不是指浮点数甚至数字,而是指一个属性到一张桌子.我的问题是如何实际使用 lat 和 lon 等于(在数据库中它们分别等于 37.7 和 -122.4).
Which, in my understanding, is essentially saying that User.lat
and User.lon
don't refer to a float or even a number, and instead an attribute to a table. My question is how to actually use what lat and lon are equal to (In the database they are equal to 37.7 and -122.4 respectively).
推荐答案
我通过获取 distanceMath 函数并将其放入我的 Users 模型类并传递它self"和math",以及引用相应的表值:
I solved it by taking the distanceMath function and placing it into my Users model class and passing it "self" and "math", as well as referring to the table values accordingly:
def distanceMath(self, lat1, lon1, math=math):
distance = math.acos(math.sin(math.radians(lat1))*math.sin(math.radians(self.lat))) + math.cos(math.radians(lat1))*math.cos(math.radians(self.lat))*math.cos(math.radians(lon1)-math.radians(self.lon))
return distance
然后在执行查询时,只需传入 current_user 作为 Users 实例,一切正常:
Then when performing the query, just pass in the current_user as the Users instance, and everything works perfectly:
users = Users.query.filter(current_user.distanceMath(lat1, lon1) == 1).all()
相关文章