Mongoengine: DateField

Created on 14 Nov 2013  路  8Comments  路  Source: MongoEngine/mongoengine

I know that there is no way to store dates in mongodb, but in code i have to.
Is it some hook, where i can set DateTimeField in Document class, but declare some pre_save/post_save method, where i can convert from date to datetime with zeros and UTC tz and backward. Or shall I better write my own DateField inheriting from BaseField?

Enhancement

Most helpful comment

No date field? Seriously?

All 8 comments

I think your own field would be best.
Perhaps you could also define as DateTimeField, but override the getters and setters to make it a Date?

Are such getters and setters hooks implemented already?

Why not store datetime and accept that it will also include seconds?

But i got dates in my code. It looks way more convenient put this date > datetime and datetime > date transformation on ODM level rather than do it in app logic.

+1 for DateField.

When you make an auto filter generator it bases on model class. And the difference between DateField or DateTimeField is significant.

For DateTime auto-generated queries will be like

{'created_on': {'$lt': datetime.datetime(2014, 6, 4, 0, 0, 1, tzinfo=...)

which obviously will "not work" with pure dates where we need

{'$gte': day, '$lt': day + 1}

It has nothing to do with how mongoDB stores data. You need to distinguish it here.
Same thing as Decimal vs Float, btw. Mongo has no decimals but we want it in Python.

So everyone will come to declaring their own DateField sooner or later.
So why just not add it to the core?

If MongoDB doesn't support date fields take it upstream (and report here when an issue was filed there) because that just plain wrong.
I'm +1 on emulating it for now since it's very useful.

I also came for the need of DateField. Any updates for this issue?

No date field? Seriously?

Was this page helpful?
0 / 5 - 0 ratings