Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: Object of type 'datetime' is not JSON serializable

Tags:

python

django

I'm trying to save a date in my sessions. I always receive the error Object of type 'datetime' is not JSON serializable. I found this here in the Django documentation: stored as seconds since epoch since datetimes are not serializable in JSON.

How can I save my expiry_date as seconds instead of datetime?

code = social_ticketing_form.cleaned_data['a']
expiry_date = timezone.now() + timezone.timedelta(days=settings.SOCIAL_TICKETING_ATTRIBUTION_WINDOW)
request.session[social_ticketing_cookie_name(request.event)] = {'code': code, 'expiry_date': expiry_date}
like image 716
Joey Coder Avatar asked Jan 26 '23 19:01

Joey Coder


1 Answers

Either write your own session serialiser to allow you to serialise datetime objects directly, or store the datetime value in some other form.

If you want to save it as seconds, then use the datetime.timestamp() method:

request.session[social_ticketing_cookie_name(request.event)] = {
    'code': code, 
    'expiry_date': expiry_date.timestamp()
}

Your own SESSION_SERIALIZER class only needs to provide loads and dumps methods, directly analogous to json.loads() and json.dumps() (which is how the standard JSON serializer is implemented).

If you want to encode datetime objects and be able to transparently turn those back into datetime objects again, I'd use a nested object format to flag such values as special:

from datetime import datetime

class JSONDateTimeSerializer:
    @staticmethod
    def _default(ob):
        if isinstance(ob, datetime):
            return {'__datetime__': ob.isoformat()}
        raise TypeError(type(ob))

    @staticmethod
    def _object_hook(d):
        if '__datetime__' in d:
            return datetime.fromisoformat(d['__datetime__'])
        return d

    def dumps(self, obj):
        return json.dumps(
            obj, separators=(',', ':'), default=self._default
        ).encode('latin-1')

    def loads(self, data):
        return json.loads(
            data.decode('latin-1'), object_hook=self._object_hook
        )

and set SESSION_SERIALIZER to the full qualified name of the above module (path.to.module.JSONDateTimeSerializer).

The above uses the datetime.fromisoformat() method, new in Python 3.7.

like image 125
Martijn Pieters Avatar answered Jan 31 '23 08:01

Martijn Pieters