Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Django Basics Model Administration What are Models?

Austin Hawkins-Seagram
Austin Hawkins-Seagram
5,720 Points

Django ORM DateTimeField: auto_now_add=True vs. __init__ datetime.datetime.now()

in Django's ORM, is models.DateTimeField(auto_now_add=True) basically just datetime.datetime.now() in the class _init_?

why is it necessary?

does _init_ not work with the ORM or the database?

(PS sorry for the weird dunder stuff, the writer confuses it with bold text)

1 Answer

Haydar Al-Rikabi
Haydar Al-Rikabi
5,971 Points

Basically, these two statements are the same:

created_at = models.DateTimeField(auto_now_add = True)
created_at = models.DateTime.Field(default = datetime.datetime.now)

Pay attention that we used datetime.datetime.now without the parenthesis () in the end. This is because we if you use the parenthesis, then the "created_at" field will be updated with a new value every time your app is run. We don't this to happen. The initial value of created_at field should not be changed once it is created.