如何在 SQLAlchemy 中的 M2M 上执行简单的 next() 方法?
我有两个多对多模型:团体模型和个人模型。
我可以访问 group.individuals 并获取相关个人的列表。我在组模型上有一个“last_individual_id”列,用于跟踪最后使用的个人。有了这些信息,我就想知道如何为一个团队找到下一个人。
我想获取个人的 id 并使用 itertools.cycle 但我无法指定起点。另外,如果我可以在 SQLAlchemy 中正确执行此操作,那么这可能是一种缓慢的方法。
关于如何实现这一目标有什么想法吗?我觉得我会因为答案如此简单而感到尴尬......但我今天没有咖啡因!
谢谢
I have two models with a many to many, Group and Individual.
I can access group.individuals and get a list of the related individuals. I have a 'last_individual_id' column on the Group models to keep track of the Last used individual. With this information I was wandering how get get the next individual for a Group.
I thought of getting the id's for the Individuals and using itertools.cycle but i can't specify the start point. Plus that may be a slow way to do it if I can just do it properly in SQLAlchemy.
Any thoughts on how to accomplish this? I feel like I will be embarrassed at how simple the answer is... but I didn't have caffeine today!
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种可能性是使用 itertools.dropwhile (但这可能会受到您所需解决方案的范围的限制,因为尚不清楚,而且 itertools.dropwhile 也不会像 itertools.cycle 那样无限循环您的列表)。
您没有提供数据模型的大量细节。我假设 group.individuals 返回个人对象的列表,并且“.id”属性代表每个人的 ID 号。例如,假设您在 group.individuals 列表中查找个人 id 7:
这将遍历列表,直到找到 id=7(或者更准确地说,直到 x.id != 7,因为 itertools.dropwhile 不会)在第一个 False 测试之前不会返回任何内容),然后它将开始从列表中返回单个对象。因此,如果该列表中的 id 类似于: [1, 19, 5, 6, 7, 2, 4, 5] 这将返回: 7 2 4 5。
出于您的目的,听起来您会忽略第一个返回的响应,然后使用之后的响应。
另一种选择是迭代您的原始 group.individuals 列表以找到您想要的 id 的索引,然后只需增加索引 - 如果您计算的索引超出您的列表,这也将允许您从 0 开始索引,例如示例:
但是,当然,这是更加离散的,并且不可迭代。可能还有很多其他方法可以解决这个问题(包括使我的函数可迭代),但我还没有喝咖啡因!
One possibility is using itertools.dropwhile (but this may be limited as the scope of your desired solution, as it is not clear, also itertools.dropwhile doesn't infinitely cycle through your list like itertools.cycle does).
You didn't provide a lot of detail of your data model. I'll presume that group.individuals returns a list of individuals objects and the '.id' property represents each individual's id number. For example, say your looking for individual id 7, within the list of group.individuals:
This will go through the list until it finds id=7 (or more acurately, until x.id != 7, as itertools.dropwhile won't return anything until the first False test), then it will start returning individuals objects from the list. So if the id's from that list are like: [1, 19, 5, 6, 7, 2, 4, 5] this would return: 7 2 4 5.
For your purposes it sounds like you would ignore the first returned response, and then use the responses after.
Another alternative is to iterate through your original list of group.individuals to find the index of the id you want, then just increment the index - this would also allow you to start the index over at 0 if your calculated index exceeds your list, for example:
But, of course, this is more discrete, and isn't iterable. There are probably lots of other ways to approach this (including making my function iterable) but I also haven't had my caffeine yet!