Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: auto_now and auto_now_add options for DateTimeField #21

Open
lig opened this issue May 16, 2012 · 18 comments
Open

Proposal: auto_now and auto_now_add options for DateTimeField #21

lig opened this issue May 16, 2012 · 18 comments

Comments

@lig
Copy link
Contributor

lig commented May 16, 2012

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.

@rozza
Copy link
Contributor

rozza commented May 16, 2012

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.

@lig
Copy link
Contributor Author

lig commented May 16, 2012

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.

@rozza
Copy link
Contributor

rozza commented May 16, 2012

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

@lig
Copy link
Contributor Author

lig commented May 16, 2012

Thank you very much!

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

@rozza
Copy link
Contributor

rozza commented Aug 24, 2012

Having to bump to 0.8 as PY3 support came early!

@csabaszilveszter
Copy link

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

@beyondliu
Copy link

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

@hygull
Copy link

hygull commented May 12, 2017

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

@metadeng
Copy link

metadeng commented Jul 8, 2017

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

@hygull
Copy link

hygull commented Jul 29, 2017

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

@rohitkhatri
Copy link

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

@oussjarrousse
Copy link

oussjarrousse commented Jun 13, 2018

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

@bagerard
Copy link
Collaborator

bagerard commented Nov 4, 2018

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

@hygull
Copy link

hygull commented Nov 5, 2018

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

@hygull
Copy link

hygull commented Nov 5, 2018

@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

Kami pushed a commit to StackStorm/mongoengine that referenced this issue Dec 19, 2019
@rafaelreuber
Copy link

This is really very useful!

@oussjarrousse
Copy link

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.

@leonballoniMakes
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests