Views.py
class EventCalenderEventListView(ListAPIView):
permission_classes=[AllowAny,]
serializer_class = serializer.EventCalenderSerializer
def get_queryset(self, *args, **kwargs):
data = json.loads(self.request.GET.get('filter'))
category = data.get('category')
query_list = Event.objects.all().values('start_date').distinct()
date_list = []
for i in query_list:
for key, value in i.items():
date_list.append(datetime.datetime.strftime(value,'%Y-%m-%d'))
datas = []
result = {}
for dates in date_list:
result[dates]= []
query = Event.objects.filter(start_date=dates).order_by('start_date')
for i in query:
result[dates].append(i)
datas.append(result)
result = {}
return datas
模型.py
class Event(BaseFields):
name = models.CharField(max_length=250)
category = models.ForeignKey(EventCategory,on_delete=models.CASCADE,null=True,blank=True)
other_event_category = models.CharField(max_length=250,null=True,blank=True)
meeting_link = models.CharField(max_length=250,null=True,blank=True)
description = models.TextField(null=True,blank=True)
start_date = models.DateField(null=True,blank=True)
end_date = models.DateField(null=True,blank=True)
serializer.py
class EventCalenderSerializer(ModelSerializer):
category = SerializerMethodField()
status = SerializerMethodField()
class Meta:
model = Event
fields = fields=('id','name','category','start_date','end_date','status')
def get_category(self, obj):
category = {}
if obj.category:
category['id'] = obj.category.id
category['name'] = obj.category.name
return category
else:
category = ""
return category
def get_status(self,obj):
current_date = datetime.strftime(datetime.today(),'%Y-%m-%d')
start_date = datetime.strftime(obj.start_date,'%Y-%m-%d')
if obj.end_date is not None:
end_date = datetime.strftime(obj.end_date,'%Y-%m-%d')
if start_date == end_date and start_date < current_date:
return "completed"
elif start_date < current_date and end_date < current_date:
return "completed"
elif start_date < current_date and end_date > current_date:
return "progress"
elif start_date == current_date:
return "progress"
else:
return "upcoming"
else:
if start_date < current_date:
return "completed"
elif start_date == current_date:
return "progress"
else:
return "upcomng"
预期输出:
[
{
"2023-06-01": [
{
"name": "event 1",
"start_date": "2023-06-01",
"end_date": "2023-06-07"
}
]
},
{
"2023-06-01": [
{
"name": "event 2",
"start_date": "2023-06-01",
"end_date": "2023-06-07"
},
{
"name": "event 3",
"start_date": "2023-06-01",
"end_date": "2023-06-07"
}
]
}
...
]
1条答案
按热度按时间4si2a6ki1#
.get_queryset
的响应是传递给.list
方法的内容,该方法将序列化并返回准备显示的数据。目前,您正在将其变形为与您的serializer
不对应的内容:此外,这与您在问题中发布的结构(如预期输出)不同,我将假设您使用代码构建的是正确的。
所以,基本上你需要做的就是序列化键值中的事件列表。我们可以通过覆盖
.list
方法来解决这个问题。然后,遍历你的 “QuerySet”(实际上是一个Array
的字典,其中包含一个带有Event
对象列表的键),以这种方式序列化这个列表并重新创建你的响应:views.py
可选
我建议你通过使用QuerySet API提供的方法来清理你的视图,并以不同的方式组织你的数据,这样我们就可以直接用
Wrapper
序列化数据:serializers.py
views.py
最后,一个基于
EventCalenderSerializer
的响应示例。如果您想要的字段与问题中预期的字段相同,则从原始序列化程序中删除相应的字段(这对以前的解决方案也有效)。