mongoengine
mongoengine copied to clipboard
Proposal: auto_now and auto_now_add options for DateTimeField
There are two very useful options for DateTimeField in Django: auto_now and auto_now_add.
From django docs: """ auto_now Automatically set the field to now every time the object is saved. Useful for "last-modified" timestamps. Note that the current date is always used; it's not just a default value that you can override.
auto_now_add Automatically set the field to now when the object is first created. Useful for creation of timestamps. Note that the current date is always used; it's not just a default value that you can override. """
I think it is very simple task to implement auto_now_add behavior at least. I understand that implementing auto_now is a bit harder. By the way the fact that MongoEngine still has no support for these options lets me think that this is done by design. Which is not right by my personal opinion.
Well I'd suggest using signals, but it all depends on how you update your documents. Doing a save in pymongo will overwrite a document and isn't atomic. We do some handling of that, to make it atomic but there is a big cost in performance involved.
You can set a default which covers auto_now_add but auto_now is tricker to implement as it may be a mass update and arguably should be left to the application developer to implement.
Well, I've just noticed once more callable defaults and see default=lambda: datetime.utcnow() variant. But may be it will be useful to have this out of the box.
And yes, I understand that implementing auto_now is a kind of scary task:)
Also, I think that documenting design principles for auto_now and auto_now_add and the reasons why there shouldn't be auto_now out of the box will be very useful.
I dont think its a scary task - should be relatively trivial, its just we'd have to hook into every save (easy) and inject into every update call - maybe unexpected for the user.
I'll schedule for post 0.7 - but it might make 0.7
Thank you very much!
Now I may try to contribute some code on this task. Not promising that I will though.
Having to bump to 0.8 as PY3 support came early!
damn... how do I deactivate this :))
So is this supported now? In which version? Thanks!
Look at the following code snippet, it will clarify.
class Post(models.Model):
title = models.CharField(max_length=50, blank=False)
description = models.TextField()
pic = models.URLField(max_length=1000) #default max_length is 200
created = models.DateTimeField(auto_now_add=True, auto_now=False)
updated = models.DateTimeField(auto_now_add=False, auto_now=True)
posted_by = models.ForeignKey(User, on_delete=models.CASCADE) # "User" => won't work
def __unicode__(self):
return self.title
@hygull it's seem like that model by django,is really supported in mongo too?
@lvtuben Yes it supports. We can check this at Using MongoDB as your primary Django database.
@hygull In which version, does it work? I am using 0.13.0 and It's not working in my case.
Why unsure of this feature if it is already implemented? And why is this issue still open?
Just to clarify, auto_now and auto_now_add don't exist in mongoengine
Can you please check https://staltz.com/djangoconfi-mongoengine/#/9
@hygull In which version, does it work? I am using
0.13.0and It's not working in my case.
Can you please check https://staltz.com/djangoconfi-mongoengine/#/9
This is really very useful!
This is very useful. I wonder why wouldn't it be part of the package... I am forced to overload the save() function for every model class to add it.
This is probably not going to be added since passed 11 years already.