Advertisement
VladNitu

convert_date

May 6th, 2023
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. from datetime import datetime, time
  2. from mongoengine import StringField, DateField, Document
  3. from utils import db
  4.  
  5.  
  6. def convert_date(py_date):
  7. """Function that converts `datetime.date` object to `datetime.datetime` object.
  8. Needed in order to persist using `mongoengine` to Atlas, as `mongoengine` is unable to automatically convert `datetime.date` to a MongoDB-compatible format.
  9. """
  10. return datetime.combine(py_date, time=time.min)
  11.  
  12. # Create your models here.
  13. class React(Document):
  14.  
  15.  
  16. url = StringField(max_length = 300)
  17. date = DateField()
  18.  
  19. def save(self, *args, **kwargs):
  20. db.vlad_collection.insert_one({
  21. '_id': self.url,
  22. 'date': convert_date(self.date),
  23. })
  24.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement