最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]sqlalchemy.orm.collections.InstrumentedList看看有哪些方法

Flask crifan 5428浏览 0评论

之前自己的一个表中的关系属性是:

class Event(db.Model):
    __tablename__ = “events”
    joiners = db.relationship(‘User’, secondary=event_joiners)

打印出来类型是:

type(firstEvent.joiners)=<class ‘sqlalchemy.orm.collections.InstrumentedList’>

现在需要:

去搞懂:

sqlalchemy.orm.collections.InstrumentedList

有哪些方法

以便于我去使用

比如去query查询之类的

sqlalchemy.orm.collections.InstrumentedList

Collection Configuration and Techniques — SQLAlchemy 1.1 Documentation

“class sqlalchemy.orm.collections.InstrumentedList

Bases: __builtin__.list

An instrumented version of the built-in list.”

python – sqlalchemy: ‘InstrumentedList’ object has no attribute ‘filter’ – Stack Overflow

What is an InstrumentedList in Python? – Stack Overflow

去改为:

joiners = db.relationship(‘User’, secondary=event_joiners, lazy=’dynamic’)

然后看看能不用用query,结果不行:

    app.logger.debug(‘firstEvent=%s, firstEvent.creator=%s, type(firstEvent.joiners)=%s, firstEvent.joiners=%s’, firstEvent, firstEvent.creator, type(firstEvent.joiners), firstEvent.joiners)

会输出:

DEBUG in db_create [db_create.py:176]:
firstEvent=<Event id=1 user_openid=oswjmv4X0cCXcfkIwjoDfCkeTVVY title=u’\u8ba8\u8bba\u65e5\u5386\u7684\u516c\u4f17\u53f7\u540d\u5b57′>, firstEvent.creator=<User nickname=u’\u793c\u8c8c’ openid=oswjmv4X0cCXcfkIwjoDfCkeTVVY avatar_static_path=img/avatar/oswjmv4X0cCXcfkIwjoDfCkeTVVY.png>, type(firstEvent.joiners)=<class ‘sqlalchemy.orm.dynamic.AppenderBaseQuery’>, firstEvent.joiners=SELECT wechat_users.openid AS wechat_users_openid, wechat_users.province AS wechat_users_province, wechat_users.avatar_url AS wechat_users_avatar_url, wechat_users.avatar_static_path AS wechat_users_avatar_static_path, wechat_users.language AS wechat_users_language, wechat_users.city AS wechat_users_city, wechat_users.country AS wechat_users_country, wechat_users.sex AS wechat_users_sex, wechat_users.nickname AS wechat_users_nickname 
FROM wechat_users, event_joiners 
WHERE :param_1 = event_joiners.event_id AND wechat_users.openid = event_joiners.joiner_openid

What is an InstrumentedList in Python? – Stack Overflow

“Yes, SQLAlchemy uses it to implement a list-like object which is aware of insertions and deletions of related objects to an object (via one-to-many and many-to-many relationships).”

sqlalchemy.orm.collections.InstrumentedList – Nullege Python Samples

那么就去找python内部的list,有哪些函数和接口

python list

Python 列表(List) | 菜鸟教程

“Python列表函数&方法

Python包含以下函数:

序号函数
1
比较两个列表的元素
2
列表元素个数
3
返回列表元素最大值
4
返回列表元素最小值
5
将元组转换为列表

Python包含以下方法:

序号方法
1
在列表末尾添加新的对象
2
统计某个元素在列表中出现的次数
3
在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)
4
从列表中找出某个值第一个匹配项的索引位置
5
将对象插入列表
6
移除列表中的一个元素(默认最后一个元素),并且返回该元素的值
7
移除列表中某个值的第一个匹配项
8
反向列表中元素
9
对原列表进行排序

Python List pop()方法 | 菜鸟教程

使用list和tuple – 廖雪峰的官方网站

->看来没法去query加上filter了

只能自己去循环中,获取每一个变量,然后去判断了。。。

curEvent = Event.query.filter_by(id=eventIdInt).first()
app.logger.debug(“curEvent=%s”, curEvent)
curEventJoiners = curEvent.joiners
app.logger.debug(“curEventJoiners=%s”, curEventJoiners)
hasJoined = False
for eachJoiner in curEventJoiners:
    if eachJoiner.openid == sourceUser.openid:
        hasJoined = True
        break
if hasJoined:
    respContent = u”您已经加入了该活动: %s” % (curEvent.title)
else:
    curEvent.joiners.append(sourceUser)
db.session.commit()
    respContent = u”已成功将您加入了活动: %s” % (curEvent.titile)

转载请注明:在路上 » [已解决]sqlalchemy.orm.collections.InstrumentedList看看有哪些方法

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.156 seconds, using 22.33MB memory