How to divide [filtered list by date(from_date, to_date)] by day. Django
i want to display information about likes.
I filtered a model (from_date, to_date) and got list of all likes during this time. Now I need to sort and divide it somehow to make it looks something like this:
[
{
date: 2021-12-10,
likes:[
{
user: user1,
post: post1
},
{
user: user1,
post: post1
}
]
},
{
date: 2021-12-11,
likes:[
{
user: user1,
post: post1
},
]
}
]
Could you please give me some advices how it is possible to do.
models.py
class LikesActivity(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
post = models.ForeignKey(Post, on_delete=models.CASCADE)
created = models.DateTimeField(auto_now_add=True)
def __str__(self):
return f"{self.post} liked by {self.user}, {self.created}"
class Meta:
verbose_name = 'Likes activity'
verbose_name_plural = 'Likes activities'
views.py
class LikesActivityRangeFilter(APIView):
def get(self, request, from_year, from_month, from_day,
to_year, to_month, to_day):
from_date = f"{from_year}-{from_month}-{from_day}"
to_date = f"{to_year}-{to_month}-{to_day}"
likes = LikesActivity.objects.filter(
created__range=[from_date, to_date]).order_by('created')
serializer = LikesActivitySerializer(likes, many=True)
return Response(serializer.data)