anc1revv
anc1revv

Reputation: 1613

Displaying Django objects by date created

I built an ecommerce site for a food delivery company. They run deliveries from 7pm-2am everynight.

I want to build a page where they can view all their orders from the beginning of time, sectioned off by each date. But a day of deliveries on Oct 8, would include the deliveries from 7pm-11:59pm on oct 8 AND the deliveries from 12:00am to 2am on Oct 9.

I have a general idea on how to filter the objects by the "date created", but I'm not sure how to aggregate all the different days and display them in a organized manner.

I'm also using python 2.6, so i can't store each day's orders in an ordered dict.

Upvotes: 0

Views: 782

Answers (1)

dani herrera
dani herrera

Reputation: 51655

EDITED

Perhaps you are looking for query range to select a slice of your data. Sample:

start_date = datetime.datetime(2012, 2, 17, 19, 00, 00, 00)
end_date = datetime.datetime(2012, 2, 23, 59, 00, 00, 00)
Deliveries.objects.filter(deliveries_date__range=(start_date, end_date))

(not tested)

But, rereading your question perhaps your problem is with presentation tier, then, in your template you should 'cut' data by day. You can check if some field change in your template with ifchange block tag:

{% for deliver in deliveries %}
    {% ifchanged deliver.deliveries_date.date %} 
            {{ deliver.deliveries_date.date }} 
    {% endifchanged %}
    ...
{% endfor %}

Upvotes: 3

Related Questions