简体   繁体   English

如何构造这个Django ORM queryset查询?

[英]how to construct this Django ORM queryset query?

I'm trying to determine how to construct a query. 我正在尝试确定如何构造查询。 I have two models from an external database that I"m trying to import into a different schema. The source data has three models: 我正在尝试从外部数据库中导入两个模型,这些模型要导入到不同的架构中。源数据具有三个模型:

class Group(model):
    ...
    name = models.CharField()

class Person(model):
    ...
    name = models.CharField()

class Membership(model):
    ...
    status = models.CharField()
    created_date = models.DateTimeField()
    modified_date = models.DateTimeField()
    person = models.ForeignKey(
        'Person',
        related_name='memberships',
    )
    group = models.ForeignKey(
        'Group',
        related_name='memberships',
    )

In the Memberships, I have fields representing the person and band, with create dates and update dates, as well as other relevant data. 在“成员资格”中,我有代表个人和乐队的字段,其中包含创建日期和更新日期以及其他相关数据。

Here's the problem: the Membership rows don't have any field that represents which is the canonical record, and they mix and match persons and dates and other data. 这就是问题所在:“成员资格”行没有任何字段代表标准记录,并且它们混合并匹配人员,日期和其他数据。

What I'm doing currently is taking all the Memberships for a given band, running distinct on them for the combination of band and person and using those results to query the Memberships again for the latest instance of each particular combination. 我目前正在做的是获取给定乐队的所有成员资格,对他们进行不同的乐队和人物组合,并使用这些结果再次查询成员资格以获取每个特定组合的最新实例。 While this works, as you might imagine is is unspeakably slow and I know there must be a better way. 尽管这可以奏效,但您可能想像的是如此缓慢,我知道必须有更好的方法。

So I'm looking for how to use some of the advanced django query expressions (Window? Over, F-expressions?) to get this done as much as possible on the database. 因此,我正在寻找如何使用一些先进的django查询表达式(Window?Over,F-expressions?)来在数据库上尽可能地做到这一点。

Here is the code that is getting me the records I want: 这是使我获得所需记录的代码:

groups = (the groups I want)
for group in groups:
    unique_members = group.members.values(
        'person',
        'group',
    ).distinct()
    for member in members:
        current_member = group.members.filter(
            person__id=member['person'],
            group__id=member['structure'],
        ).lastest('created_date', 'modified_date')

With current_member being the most recent membership entry for that unique person/group combination. 使用current_member是该唯一个人/组组合的最新成员资格条目。

So for a list of memberships like this: 因此,对于这样的成员列表:

Person           Group        Created     Status
John Lennon      Beatles      1960-01-01  Current
Paul McCartney   Beatles      1960-01-01  Current
Pete Best        Beatles      1960-01-01  Expired
George Harrison  Beatles      1960-01-01  Current
Ringo Starr      Beatles      1962-01-01  Expired
Pete Best        Beatles      1964-01-01  Expired
Ringo Starr      Beatles      1966-01-01  Current

I'd want the following list: 我想要以下列表:

John Lennon      Beatles      1960-01-01  Current
Paul McCartney   Beatles      1960-01-01  Current
Pete Best        Beatles      1964-01-01  Expired
George Harrison  Beatles      1960-01-01  Current
Ringo Starr      Beatles      1966-01-01  Current

Generated from a single query. 从单个查询生成。

Thanks! 谢谢!

You can first order your members query by the desired entry then get the distict values. 您可以先按所需条目对成员查询进行排序,然后获取Distict值。 This question may help you: 这个问题可以帮助您:

Django query: get the last entries of all distinct values by list Django查询:按列表获取所有不同值的最后一个条目

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM