0

我有 3 个看起来像这样的模型(简化):

用户:

id
username

资源:

id
name
description

评论:

id
user_id (relationship User)
resource_id (relationship Resource)
data
date_created

我正在尝试查询用户的评论并按资源对其进行分组。我希望结果返回为:[(Resource A, [Comment, Comment, Comment, ...]), (Resource B, [Comment, Comment, ...]), (Resource X, [Comment ])]

我已经尝试了各种构建它的方法,但我似乎无法弄清楚。做这样的事情的正确方法是什么?

编辑

现在代码如下所示:

contrib = db_session.query(Resource).filter(Comment.user==user, Resource.uuid==Comment.resource_id).distinct(Comment.resource_id).order_by(desc(Comment.date_created))
comments = db_session.query(Comment, Resource).filter(Comment.user==user, Comment.resource_id.in_([r.uuid for r in contrib]), Resource.uuid==Comment.resource_id).order_by(desc(Comment.date_created))

然后我使用一些列表/字典理解将这些结果组合成看起来像的东西

[{resource: Resource, comments:[Comment, Comment, Comment]}, {resource: Resource, comments:[Comment, .....]}, .....]

必须有更好的方法来做到这一点!

4

2 回答 2

1

您可以使用自定义MappedCollection对评论进行分组:

from sqlalchemy.orm.collections import collection, MappedCollection

class GroupedCollection(MappedCollection):

  def __init__(self):
    super(GroupedCollection, self).__init__(
      self,
      lambda e: e.resource_id # the key we want to group by
    )

  @collection.internally_instrumented
  def __setitem__(self, key, value, _sa_initiator=None):
    if key in self:
      # there is already another comment for that resource
      # we simply append the comment (or you could do something
      # more fancy here if you would like to order the comments)
      self[key]['comments'].append(value)
    else:
      # we create a new entry with a dictionary containing the
      # resource and comment
      super(GroupedCollection, self).__setitem__(
        key,
        {'resource': value.resource, 'comments': [value]},
        _sa_initiator
      )

然后在你的User类上添加对应关系:

class User(Base):

  # ...

  grouped_comments = relationship(
    'Comment',
    collection_class=GroupedCollection
  )

访问它会给你按资源分组的评论:

>>> user.grouped_comments
{
  'resource_id_1': {'resource': <Resource 1>, 'comments': [<Comment ...>, <Comment ...>]},
  'resource_id_2': {'resource': <Resource 2>, 'comments': [<Comment ...>]}
}
>>> user.grouped_comments.values()
[
  {'resource': <Resource 1>, 'comments': [<Comment ...>, <Comment ...>]},
  {'resource': <Resource 2>, 'comments': [<Comment ...>]}
]

请注意,这种关系只能用于查看相关模型,启用添加/删除模型需要额外的工作。

最后,如果这是您想要重现的模式,您可以轻松地创建一个GroupedCollection工厂函数,您可以在其中指定分组键。

于 2013-02-19T04:54:31.727 回答
0

请查看演示:http ://sqlfiddle.com/#!2/9f2ea/2

希望这可以帮助

于 2013-02-18T16:36:11.353 回答