我有一个如下所示的事件模型:
class Event(models.Model):
display_name = models.CharField(max_length=254)
# Period
date_start = models.DateTimeField()
date_end = models.DateTimeField()
# Address
address = models.CharField(max_length=254)
postal_code = models.CharField(max_length=254)
city = models.CharField(max_length=254)
我想根据date_start
字段的日期值对事件进行分组。然后像这样使用Django REST框架回复JSON数据:
{
data: [
{
date: "yyyy-mm-dd",
items: [
{
display_name: "Event 1",
date_start: "yyyy-mm-dd'T'HH:mm:ss",
date_end: "yyyy-mm-dd'T'HH:mm:ss",
address: "...",
postal_code: "...",
city: "..."
},
{
display_name: "Event 3",
date_start: "yyyy-mm-dd'T'HH:mm:ss",
date_end: "yyyy-mm-dd'T'HH:mm:ss",
address: "...",
postal_code: "...",
city: "..."
}
]
},
{
date: "yyyy-mm-dd",
items: [
{
display_name: "Event 2",
date_start: "yyyy-mm-dd'T'HH:mm:ss",
date_end: "yyyy-mm-dd'T'HH:mm:ss",
address: "...",
postal_code: "...",
city: "..."
}
]
}
]
}
我是Python和DRF的新手。有人可以帮我吗?