I have data in Postgres DataBase like this based on my Model Event
| id | name | start_date | end_date |
1 Event1 2018-09-14 14:22:00 2018-09-15 14:22:00
2 Event2 2018-09-15 14:22:00 2018-09-15 15:22:00
I need to return response group_by date and If Event duration (end_date, start_date) took 2 days so i need return him twice in two days and this all should be order by date. So response should look like this:
{
"2018-09-14": [
{
"id": 1,
"name": "Event1",
"start_date": "2018-09-14 14:22:00",
"end_date": "2018-09-15 14:22:00",
}],
"2018-09-15": [{
"id": 1,
"name": "Event1",
"start_date": "2018-09-14 14:22:00",
"end_date": "2018-09-15 14:22:00",
},
{
"id": 2,
"name": "Event2",
"start_date": "2018-09-15 14:22:00",
"end_date": "2018-09-15 15:22:00",
}]
}
How can i do this?
Your example data isn't a Django Model, its just a regular Python
dict
.You could simply iterate over it and build the required results
dict
.Ok. I find decision for this
1) First i find SQL that i need for this
2) Second i write this on Django ORM:
So i get list that i want:
This response is nice for me.