搜索期间访问Flask-Sqlalchemy查询中的表变量 [英] Accessing table variables in Flask-Sqlalchemy query during search

查看:41
本文介绍了搜索期间访问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)

表中还有其他元素,但是lat和lon最相关.我有一个方法 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 方法:

# 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()

这篇关于搜索期间访问Flask-Sqlalchemy查询中的表变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆