1

I'm refactoring a legacy Django Job to use annotate with filtered Count aggregations instead of querying each record individually (avoiding the N+1 problem).

I want to count the number of related EventReport objects per Store, excluding those where status="C".

So I wrote something like:

stores_with_monitoring_enabled.annotate(
 total_cards=Count(
 'eventreport',
 filter=Q(
 eventreport__event_at__gte=day_30_days_ago_start,
 eventreport__event_at__lte=yesterday_end
 ) & ~Q(eventreport__status='C')
 ),
# ... etc

But Django raised SyntaxError: positional argument follows keyword argument.

I also tried:

# ... etc
filter=Q(
 eventreport__event_at__gte=start_date,
 eventreport__event_at__lte=end_date
) & ~Q(eventreport__status="C")
# ... etc

But I'm unsure if this is the correct pattern inside annotate()'s filter.

I expected to get only related objects where `status != "C" without any errors.

PS: I looked into other solutions on StackOverflow and the suggestions on this one: How do I do a not equal in Django queryset filtering?, but I could'nt get it working when using Q() alongside ~Q() with other kwargs.

What’s the best approach to express status != 'C' inside a Count(..., filter=...) clause?

asked Jul 8 at 14:30
2
  • 1
    Can you share the full traceback? I think it is located somewhere else. Commented Jul 8 at 14:54
  • always put full error message because there are other useful information. Commented Jul 8 at 18:43

1 Answer 1

3

But Django raised SyntaxError: positional argument follows keyword argument.

No, Python raises this error. Likely because you have other, non-named parameters, for example:

stores_with_monitoring_enabled.annotate(
 total_cards=Count(
 'eventreport',
 filter=Q(
 eventreport__event_at__gte=day_30_days_ago_start,
 eventreport__event_at__lte=yesterday_end
 ) & ~Q(eventreport__status='C')
 ),
 (削除) Count('some_other_count') (削除ここまで),
)

now in Python, you put positional parameters always before named parameters, so f(x, y=z) is valid, (削除) f(y=z, x) (削除ここまで) is not, you thus should inspect the method call that raises the error, and look for such patterns.

answered Jul 8 at 18:53
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.