class UserEncoder(json.JSONEncoder):
def default(self, z):
if isinstance(z, datetime.datetime):
return str(z)
elif isinstance(z, Group):
return str(z)
else:
return super().default(z)
print(json.dumps(model_to_dict(request.user), indent=4, sort_keys=True, cls=UserEncoder))
print(json.dumps(request.session.get('attributes', None), indent=4, sort_keys=True, cls=UserEncoder))