Joey Coder
Joey Coder

Reputation: 3489

Django: Query for __range

I created the following query but receive the following error SyntaxError: positional argument follows keyword argument. The problem seems to be created__range(start_date, end_date). created is a timestamp saved in the following format: 2019-12-10 21:01:58.309276+00

start_date = datetime.date(2019, 10, 1)

# Filtering a DateTimeField with dates won’t include items on the last day,
# because the bounds are interpreted as “0am on the given date”.
end_date = datetime.date(2019, 11, 1)

Order.objects.filter(
    event__organizer=176,
    created__range(start_date, end_date),
    status__in=(
        OrderStatus.PAID,
        OrderStatus.REFUNDED,
        OrderStatus.PARTIALLY_REFUNDED,
    )
).count()

Upvotes: 0

Views: 495

Answers (2)

Zaki Ahmed
Zaki Ahmed

Reputation: 162

To be more accurate... try with "__date__range" ... and add an "=" sign after "range"

Try with:

start_date = datetime.date(2019, 10, 1)

end_date = datetime.date(2019, 11, 1)


Order.objects.filter(
    event__organizer=176,
    created__date__range=(start_date, end_date),
    status__in=(
        OrderStatus.PAID,
        OrderStatus.REFUNDED,
        OrderStatus.PARTIALLY_REFUNDED,
    )
).count()

Upvotes: 0

Pedram
Pedram

Reputation: 3920

You are missing the = operator:

start_date = datetime.date(2019, 10, 1)

# Filtering a DateTimeField with dates won’t include items on the last day,
# because the bounds are interpreted as “0am on the given date”.
end_date = datetime.date(2019, 11, 1)

Order.objects.filter(
    event__organizer=176,
    created__range=(start_date, end_date),
    status__in=(
        OrderStatus.PAID,
        OrderStatus.REFUNDED,
        OrderStatus.PARTIALLY_REFUNDED,
    )
).count()

Upvotes: 2

Related Questions