Django REST框架:返回分组的QuerySet

时间:2018-09-25 06:25:53

标签: python django-rest-framework django-views django-queryset django-serializer

我有一个如下所示的事件模型:

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的新手。有人可以帮我吗?

0 个答案:

没有答案