简体   繁体   English

Django计算字段中的唯一项目并通过字段选择按父字段分组

[英]Django count unique items in field and group by parent field with field selection

I'm using Django 1.10, python 2.7, mysql and I'm trying to get a count of each unique value in a particular field, but I would like a summary count for each of the parent instances.我正在使用 Django 1.10、python 2.7、mysql,并且我正在尝试获取特定字段中每个唯一值的计数,但我想要每个父实例的汇总计数。 I'm fairly new to Django and database design so please do let me know if I've got my structure wrong.我对 Django 和数据库设计还很陌生,所以如果我的结构有误,请告诉我。

ie I have two models in question here即我这里有两个模型有问题

TestRun:
branch - Branch test was run on - charField
requester - Who initiated test - charField
commit - What commit id it is. - integerField
instance - differentiate between rerun of same commit - integerField

Each Testrun can have multiple test results每个 Testrun 可以有多个测试结果

TestResult:
testrun - foreign key
testcase = charField
commit - same as in test run - duplicated for performance reasons
result = charField i.e pass, fail, blocked, errored etc

Now I would like to get a summary of results by instance: ie现在我想通过实例获得结果摘要:即

 [{instance: 5, commit: 28000, resquester: bob, pass: 5,  fail: 4, blocked: 4, errored: 6},
  {instance: 500, commit: 28100, requester: sally, pass: 2, fail:3, blocked: 8, errored: 3}]

The method I have working so far is as follows:我到目前为止工作的方法如下:

  1. Get distinct list of commits获取不同的提交列表
  2. For each commit get unique instances对于每个提交获得唯一的实例
  3. For each instance use python Counter for all results matching instance to get summary for instance.对于每个实例,对所有匹配实例的结果使用 python Counter 来获取实例的摘要。 Append to a list.附加到列表中。

The actual implementation I have is as follows.我的实际实现如下。 data_set is filtered by query params ie branch数据集由查询参数过滤,即分支

commits = data_set.values_list('commit', flat=True).distinct()
for commit in commits:
    commit_data = data_set.filter(commit=commit).prefetch_related('testrun')
    instances = set()
    for tr in commits:
        instances.add((tr.test_run.instance, tr.test_run.id))
    for instance, tr_id in instances:
        summary = dict(Counter(obj.result for obj in commit_data if obj.testrun.instance == instance))
        data.append({'commit': commit, 'instance': instance, 'summary': summary, 'tr_id': tr_id})

The code works fine but it doesn't really scale.代码工作正常,但它并没有真正扩展。 I have looked into the grouping that Django now has but I'm not quite able to group it this way.我已经研究了 Django 现在拥有的分组,但我不太能够以这种方式对其进行分组。 I have tried two ways:我尝试了两种方法:

  1. Got me a summary of results for all my data为我提供了所有数据的结果摘要

     TestResult.objects.filter(test_run__branch=branch).values('result').annotate(count=Count('result'))

    [{'count': 400, 'result': 'blocked'}, {'count': 250, 'result': 'errored'} ...] [{'count': 400, 'result': 'blocked'}, {'count': 250, 'result': 'errored'} ...]

  2. The second got me a summary for each unique result but the summary was not not combined into a dictionary第二个给了我每个唯一结果的摘要,但该摘要没有合并到字典中

     TestResult.objects.filter(test_run__branch=branch).values('result', 'test_run__commit', 'test_run__instance').annotate(count=Count('result'))

    [{'count': 3, 'result': 'blocked', 'instance': 5, 'commit': 2400}, {'count': 6, 'result: 'errored', 'instance': 5, 'commit': 2400}....] [{'count': 3, 'result': 'blocked', 'instance': 5, 'commit': 2400}, {'count': 6, 'result: 'errored', 'instance': 5, '提交':2400}....]

These two are two the closest I've gotten to, but they are not quite there.这两个是我最接近的两个,但它们并不完全在那里。 I do know that when you use annotate with values preceding, the values determine the grouping, but what happens to field selection?我知道当您使用带有前面的值的 annotate 时,这些值决定了分组,但是字段选择会发生什么? How can you get both field selection and grouping together when doing a summary?在进行汇总时,如何同时进行字段选择和分组? Is there a better Django way then my current implementation.有没有比我目前的实现更好的 Django 方式。

I think this will do the trick:我认为这可以解决问题:

TestRun.objects.
filter(branch=branch).values('commit', 'requester', 'instance').
annotate(result=F('testresult__result'), count=Count('testresult__result'))

Let me know if it worked for you!让我知道它是否对你有用!

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

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