Alex Rivera | Logout

How can my django model DateField add 30 days to the provided value?

Asked 2010-02-04T10:51:26.903
34

as the title suggests. I want to add 30 days to the DateField field. This is auto populated on creation of record using auto_now_add=True

Any ideas how to go about doing this?

Thanks

Edit
Report

1 Answer

5

Use Pythons timedelta:

from datetime import timedelta
d = timedelta(days=30)

# object is your current instance of the model
object.yourdatefield += d
# or this because I am not sure whether the previous works
object.yourdatefield = object.yourdatefield + d

object.save()

And from the Django documentation:

DateField
A date, represented in Python by a datetime.date instance.

If you want to add 30 days on creation of the object, forget about auto_now_add=True and do as becomingGuru suggests. Information about overriding save() can also be found in the Django documentation.

answered 2010-02-04T11:00:31.337

Your Answer