The json data format I want like this:
[
{
monthName: "AUG 2019",
items: [
{
day: "03",
description: "Baseball",
...
For further actions, you may consider blocking this person and/or reporting abuse
i have a solution that I've tried and worked for me.
models.py
class Item(models.Model): time = models.IntegerField() description = models.CharField(max_length=50) class GroupData(models.Model): item = models.ForeignKey(Item, related_name='items', on_delete=models.CASCADE) date = models.DateField()serilizer.py
import datetime class GroupDataSerializer(serializers.ModelSerializer): date = serializers.SerializerMethodField(method_name='get_date') class Meta: model = GroupData fields =('__all__') def get_date(self, instance): date = datetime.datetime.now() return date.strftime("%m/%d/%Y")views.py
class GroupDataView(generics.ListAPIView): queryset = GroupData.objects.all() serializer_class = GroupDataSerializer def get_queryset(self): queryset = self.queryset return queryset.objects.filter(data=id).order_by('-date')hope it works for you. DM me if you have a problem
My Model Is
class Event(models.Model):
title = models.CharField(max_length=255, default=None, unique=True)
description = models.TextField()
date = models.DateField(default=None, null=True)
I want to group the from the date field with month-year( like 01-2017 or JAN-2017)
I see that you created another model for that. Can I do it with single model?
yeah you can but as a best practice if you have a data that require more than one field just put it in a class and refer to it as a foreign key
so it will be a class for your event that have date and name
and a class for group of event or category that take events as foreign keys
in django admin you can control that easily
Ok. Let me try this. If it is worked that would be great
Here are my codes
models.py
views.py
Serializer.py
can you please tell me why same month showing twice with same data?
models.py
serializer.py
views.py
results
it should be like this