-
Notifications
You must be signed in to change notification settings - Fork 55
Datetime object #496
Comments
Please consider, that the In your sample code, a custom decoder: Furthermore, I am really not suggesting to store only 1 date in a document, better to use a field for this purpose, for example: val = document['date'].strftime("%Y-%m-%d %H:%M:%S") But in this case also the custom decoder should be able to assign the |
The example url is what I am using. It does not work with the above code. Performing: Printing document['date'] displays a dict of the endcoded timedate object. Decode doesnt seem to be applying. Reading over the base code quick like, I dont see anything about decoding the object only encoding. Is this a bug then? I assume this should read something like self._client.decoder and not whatever that is? client.py has a gobal encoder that looks to be spread out for the whole program, but not one for decoding. Yea just tested, seem to fix the issue when I make the changes and then when calling first time passing the decoder along with the encoder seems to work across the board? |
No, there is no bug, a custom decoder can be attached to the document handling and it will be applied on the JSON content after the content is fetched: python-cloudant/src/cloudant/document.py Line 167 in 3378146
The DateTimeEncoder encodes Python If you want to decode the doc = Document(db, 'docid', encoder=DateTimeEncoder, decoder=DateTimeDecoder)
doc.fetch() # decoder makes the data manipulation and should convert `dict` to `datetime`
print(doc['date']) # prints a date string 2021-04-29 11:24:42.812545
print(type(doc['date'])) # <class 'datetime.datetime'> because the `dict` has been converted to `datetime` by the decoder
db['docid']['date'] = datetime.now()
db['docid'].save() # encode makes data manipulation and `datetime` should be converted to `dict` format If you are accessing the document via the database db['docid'] = Document(db, 'docid', encoder=DateTimeEncoder, decoder=DateTimeDecoder)
db['docid'].fetch()
db['docid']['date'] = datetime.now()
db['docid'].save()
print(db['docid']['date']) # prints a date string 2021-04-29 11:24:42.812545
print(type(db['docid']['date'])) # <class 'datetime.datetime'>
db['docid'].fetch() # overwrites the local entry content with the remote
print(db['docid']['date']) # still prints a date string 2021-04-29 11:24:42.812545
print(type(db['docid']['date'])) # still a <class 'datetime.datetime'>
The |
How to deal with time objects?
with Document( self.app.master_database["something"], id, encoder=DateTimeEncoder, decoder=DateTimeDecoder ) as document:
val = document["created_on"].strftime("%Y-%m-%d %H:%M:%S")
AttributeError: 'dict' object has no attribute 'strftime'
So it looks like it is not decoding correctly on the object? As for the decoder I am just using the basic json one and looks like it is encoding correctly on the timedate object, but not decoding at all? What am I missing for this step?
Also, trying to wrap my head around the way this works because something is a bit off...
Setting the database to this object
self.client_user_contact_db = self.master_database.create_database( "client_user_contacts" )
Then calling self.client_user_contact_db[id]["test"] = False
vs
for entry in self.client_user_contact_db:
print(entry)
Results in test (1) to be False, but the entry test to be still True
While I know I need to call .save() I would assume still that they are the same object and I should be able to call .save any time to update the database then while I am working with the local cache version. Also, why is self.client_user_contact_db giving me back whole documents vs just the keys? I can check for keys eg if key in self.client_user_contact_db, but I would think it too would just give me back keys for the for loop - keys being the ids.
The text was updated successfully, but these errors were encountered: