How to Convert Python datetime to firestore timestamp format

'Cannot convert to a Firestore Value', datetime.date(2004, 1, 16)





raise TypeError(

TypeError: ('Cannot convert to a Firestore Value', datetime.date(2004, 1, 16), 'Invalid type', <class 'datetime.date'>)



Related documentation


https://firebase.google.com/docs/firestore/manage-data/data-types 

https://code.djangoproject.com/ticket/16312

 def to_json(self):

137             import pdb;pdb.set_trace()

138             json = {

139  ->             "id" : self.id,

140                 "vehicle_merk" : self.vehicle_merk.name,

141                 "vehicle_model": self.vehicle_model.name,

142                 "price": str(self.price),

143                 "created": self.created , #strftime("%Y-%m-%dT%H:%M:%SZ"), # strftime("%Y_%m_%d_%H_%M_%S_%f")

144                 "fuel_type": self.fuel_type,


(Pdb) type(self.created)

<class 'datetime.datetime'>


(Pdb) type(self.first_registration)

<class 'datetime.date'>


The issue was with using a `DateField` in Django instead of a `DateTimeField`. The `DateField` type does not get automatically converted to Firestore's timestamp format, requiring a change in the field type.

For example, if you have a Django model with a `DateField` that you want to store in Firestore, you can convert it to a `DateTimeField` by adding a default time value, and then convert it to a Firestore timestamp. Here's an example:




Comments