j2labs/dictshield — Issue #44: for_json attempting to apply isoformat to a string
Author: alexanderdean
Created: 2011-12-02 14:29:58+00:00
State: open

This is with the latest version in pip. Test script:

#!/usr/bin/env python

from dictshield.document import Document, EmbeddedDocument
from dictshield.base import UUIDField
from dictshield.fields import DateTimeField, EmbeddedDocumentField
from datetime import datetime

class TestRepresentation(EmbeddedDocument):
id = UUIDField(required=True)
created_at = DateTimeField(required=True)

def __init__(self, id, created_at):
super(EmbeddedDocument, self).__init__() # Need to call Document constructor
self.id = id
self.created_at = created_at

class RootRepresentation(Document):
test = EmbeddedDocumentField(TestRepresentation, required=True)

def __init__(self, test):
super(Document, self).__init__() # Need to call Document constructor
self.test = test

test = TestRepresentation("c3491590-1ce9-11e1-8bc2-0800200c9a66", datetime.now())
print test.make_json_ownersafe(test)

root = RootRepresentation(test)
print root.make_json_ownersafe(root)

Output:

{"created_at": "2011-12-02T14:28:31.239244"}
Traceback (most recent call last):

File "/usr/local/lib/python2.7/dist-packages/dictshield/document.py", line 176, in
field_converter = lambda f, v: cls._fields[f].for_json(v)
File "/usr/local/lib/python2.7/dist-packages/dictshield/fields/base.py", line 362, in for_json
v = DateTimeField.date_to_iso8601(value)
File "/usr/local/lib/python2.7/dist-packages/dictshield/fields/base.py", line 351, in date_to_iso8601
iso_dt = dt.isoformat()
AttributeError: 'str' object has no attribute 'isoformat'