【问题标题】:Flask returning query烧瓶返回查询
【发布时间】:2016-06-07 05:50:58
【问题描述】:

我有这个型号

class Social(db.Model):
__tablename__ = 'social_auth_usersocialauth'
id = db.Column('id',db.Integer, primary_key=True)
provider = db.Column('provider',db.String(32))
extra_data = db.Column('extra_data',db.String())
uid = db.Column('uid',db.String(255))

def __init__(self,id=None, provider=None, extra_data=None, uid=None):
    self.id = id
    self.provider = provider
    self.extra_data = extra_data
    self.uid = uid
def __repr__(self):
    return '<Social %r>' % self.uid

那么这是我的功能

test = Social.query.filter(Social.uid == current_user)

当我进入这样的视野时 {{ test }}

我得到它的查询,我想得到结果,我该怎么做? 这是我现在看到的结果:

SELECT social_auth_usersocialauth.id AS social_auth_usersocialauth_id, social_auth_usersocialauth.provider AS social_auth_usersocialauth_provider, social_auth_usersocialauth.extra_data AS social_auth_usersocialauth_extra_data, social_auth_usersocialauth.uid AS social_auth_usersocialauth_uid FROM social_auth_usersocialauth WHERE social_auth_usersocialauth.uid = :uid_1

【问题讨论】:

  • 那个模型只是我模型文件的一部分:)
  • 您不需要在查询末尾添加.first() 吗?
  • 我想获取所有相同的电子邮件,我使用社交身份验证,所以用户可以在数据库中拥有超过 1 封电子邮件,所以我添加了 .all()
  • 我也遇到了这个错误,,, sqlalchemy.exc.ProgrammingError ProgrammingError: (psycopg2.ProgrammingError) can't adapt type 'LocalProxy' [SQL: 'SELECT social_auth_usersocialauth.id AS social_auth_usersocialauth_id, social_auth_usersocialauth.provider AS social_auth_usersocialauth_provider, social_auth_usersocialauth.extra_data AS social_auth_usersocialauth_extra_data, social_auth_usersocialauth.uid AS social_auth_usersocialauth_uid \nFROM social_auth_usersocialauth \nWHERE social_auth_usersocialauth.uid = %(uid_1)s'] [参数:{'uid_1':bla

标签: database postgresql flask flask-sqlalchemy


【解决方案1】:

你有两个问题。让我们从错误开始。

在您的查询中,您将uid 字段与当前用户进行比较。假设 Social 是您的用户模型,您的查询应该包括

Social.query.filter(Social.uid == current_user.uid)

您的另一个问题是如何访问查询结果。 filterfilter_by 之类的方法返回 BaseQuery(http://flask-sqlalchemy.pocoo.org/2.1/api/#flask.ext.sqlalchemy.BaseQuery)。 BaseQuery 对象有几种方法可以根据您的需要公开结果。在这种情况下,听起来你想要all(http://flask-sqlalchemy.pocoo.org/2.1/api/#flask.ext.sqlalchemy.BaseQuery.all)。

把这些放在一起就得到了

test = Social.query.filter(Social.uid == current_user.uid).all()

一旦在您的模板中,您可能想要迭代 test 而不仅仅是直接回显它。

{% for user in test %}
  {{ user }}
{% endfor %}

如果您尚未定义Social.__str__,则输出不会很有用,但这应该足以让您朝着正确的方向前进。

【讨论】:

  • 谢谢大佬,说的这么清楚,希望我的问题能早日解决... :)
  • 哎呀,顺便说一句,我收到此错误 ProgrammingError: (psycopg2.ProgrammingError) can't adapt type 'LocalProxy' [SQL: 'SELECT social_auth_usersocialauth.id AS social_auth_usersocialauth_id, social_auth_usersocialauth.provider AS social_auth_usersocialauth_provider, social_auth_usersocialauth。 extra_data AS social_auth_usersocialauth_extra_data, social_auth_usersocialauth.uid AS social_auth_usersocialauth_uid \nFROM social_auth_usersocialauth \nWHERE social_auth_usersocialauth.uid = %(uid_1)s'] [参数:{'uid_1': blablabla@blablabla.com}]
  • 我的答案的第一部分解决了这个问题。您不能将模型实例与字符串字段进行比较。您需要比较一个字符串(例如,Social.uid)。
  • 嘿,解决了 :) 在我的模式结束时,我有这个,def __repr__(self): # return '' % self.uid return self.uid 所以当我循环它,我只是得到uid,我怎样才能从中获取所有数据?包括提供者、extra_data 等其他列? :) 谢谢。
  • 如果您希望 repr 提供给您的不仅仅是 uid,请将其更改为提供给您的不仅仅是 uid。
猜你喜欢
  • 1970-01-01
  • 2021-04-09
  • 1970-01-01
  • 1970-01-01
  • 2021-06-03
  • 2019-03-23
  • 2017-09-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多