mongoengine icon indicating copy to clipboard operation
mongoengine copied to clipboard

Proposal: auto_now and auto_now_add options for DateTimeField

Open lig opened this issue 13 years ago • 18 comments
trafficstars

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.

lig avatar May 16 '12 12:05 lig

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.

rozza avatar May 16 '12 13:05 rozza

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.

lig avatar May 16 '12 13:05 lig

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

rozza avatar May 16 '12 13:05 rozza

Thank you very much!

Now I may try to contribute some code on this task. Not promising that I will though.

lig avatar May 16 '12 13:05 lig

Having to bump to 0.8 as PY3 support came early!

rozza avatar Aug 24 '12 09:08 rozza

damn... how do I deactivate this :))

csabaszilveszter avatar Sep 03 '14 19:09 csabaszilveszter

So is this supported now? In which version? Thanks!

beyondliu avatar Jul 27 '15 21:07 beyondliu

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 avatar May 12 '17 13:05 hygull

@hygull it's seem like that model by django,is really supported in mongo too?

metadeng avatar Jul 08 '17 14:07 metadeng

@lvtuben Yes it supports. We can check this at Using MongoDB as your primary Django database.

hygull avatar Jul 29 '17 06:07 hygull

@hygull In which version, does it work? I am using 0.13.0 and It's not working in my case.

rohitkhatri avatar Aug 11 '17 08:08 rohitkhatri

Why unsure of this feature if it is already implemented? And why is this issue still open?

oussjarrousse avatar Jun 13 '18 16:06 oussjarrousse

Just to clarify, auto_now and auto_now_add don't exist in mongoengine

bagerard avatar Nov 04 '18 20:11 bagerard

Can you please check https://staltz.com/djangoconfi-mongoengine/#/9

hygull avatar Nov 05 '18 06:11 hygull

@hygull In which version, does it work? I am using 0.13.0 and It's not working in my case.

Can you please check https://staltz.com/djangoconfi-mongoengine/#/9

hygull avatar Nov 05 '18 06:11 hygull

This is really very useful!

rafaelreuber avatar May 08 '20 03:05 rafaelreuber

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.

oussjarrousse avatar Jan 13 '21 15:01 oussjarrousse

This is probably not going to be added since passed 11 years already.

leonballoniMakes avatar Jul 03 '23 11:07 leonballoniMakes