The json data format I want like this:
[
{
monthName: "AUG 2019",
items: [
{
day: "03",
description: "Baseball",
},
{
day: "08",
description: " Baseball 2"
},
]
},
{
monthName: "Sep 2019",
items: [
{
day: "03",
description: "Baseball",
},
{
day: "08",
description: " Baseball 2"
},
]
}
]
Top comments (11)
i have a solution that I've tried and worked for me.
models.py
serilizer.py
views.py
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
check you data base from django admin maybe you duplicate the data !
there is no error in the code
does it worked ??
No.
It's not working for me.
return queryset.objects.filter(data=id).order_by('-date')
where you get the data=id?
date = datetime.datetime.now()
return date.strftime("%m/%d/%Y")
I need the date from date field. Not from the datetime.now()
okay the id is a field that django auto add in your model as a primary key you can made you custom primary key look in the documentation.
ah so you need to delete the datetime.now and just return the date forma from you model it should look like this
return instance.date.strftime("%m/%d/%Y")
change the date variable by you varibale name