In one of the model i have set one timestamp field as follows:
created_datetime = models.DateTimeField(auto_now_add = True)
While in shell i am able to create a obj and save it, however in my application it is raising a exception that created_datetime field cannot be null.
Confused where things went wrong!! How to reslove it.
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. So even if you set a value for this field when creating the object, it will be ignored.
auto_now - updates the value of field to current time and date every time the Model. save() is called. auto_now_add - updates the value with the time and date of creation of record.
As far as I know, best practice for default datetimes is to use the following:
created_datetime = models.DateTimeField(default=datetime.datetime.now)
Don't forget to import datetime
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With