0

I have a one to many relation between session and camp. Now I have to get the max and min dates of all camps combined for a particular session.

I am able to do it like this:

sess = Session.objects.last()
max_min_dates = sess.camp.aggregate(Min('start_date'), Max('end_date'))

But if I try to send this from HttpResponse then I am getting this error:

TypeError: Object of type 'date' is not JSON serializable

So I need to send the formatted date values in that. How can I modify the above code to get the same?

user001
  • 425
  • 4
  • 21
  • Some useful info here: https://stackoverflow.com/questions/11875770/how-to-overcome-datetime-datetime-not-json-serializable Otherwise, show more of your code so we can see what you are trying to do. – MattG Jun 08 '21 at 14:13

1 Answers1

0

The default encoder for json.dumps() does not support date encoding (ie. can't convert date into str). You can use django encoder instead, it supports a few more data types see the link for more info.

Django Ref

import json
from django.core.serializers.json import DjangoJSONEncoder

json_str = json.dumps(max_min_dates, cls=DjangoJSONEncoder)
Du D.
  • 5,062
  • 2
  • 29
  • 34