Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django aggregation and filtering result

in Django after aggregation.
if I use filter on a column that is not aggregated,
I get it used in the where clause of the SQL query
and not in having as expected.

example follows:

I have a table for test results. Let's say I have TestA and TestB.

Version:   2    |   2   |   2
TestA   :Pass   |Null   |Fail
testB   :Error  |Fail   |Null

Each test can be run at any time,
I want to display the latest result for that test.

what I've tried

x=Site.results.filter(timeEnd__isnull=False).values('idTest').annotate(Max('timeEnd'))

and then filtering x using:

x.filter(result=<number of result>)

but I get results using the second filter that are not seen in original x.

How can I get the desired result?

but if I try to display:

result(Version=2).failedFilter()
TestA=Fail
TestB=Fail

result(Version=2).PassedFilter()
TestA=Pass

result(Version=2).ErrorFilter()
TestB=Error

result(version=2)
TestA=Fail
TestB=Fail

when actually besides the Failed Filter all the rest needs to be empty.

summary:

tables:
test
----
    id
    name

Site
----
    id
    name

testresult
----------
    id
    date
    testid
    siteid
    result(int)

I want to get last result for a site with each test and then filter it by result.

like image 274
shevski Avatar asked Mar 02 '26 07:03

shevski


1 Answers

latest() will return the most recent model in a queryset.

site = Site.objects.get(id=1)
for test in Test.objects.all():
        test_result = TestResult.objects.filter(site=site, test=test).latest('timeEnd')
        print test_result.result

annotate() just calculates a value, it does not do any filtering. I think your current query will return any tests that have timeEnd set, rather than just the last one (which is what I think you want).

like image 91
Mike Ryan Avatar answered Mar 03 '26 19:03

Mike Ryan