| 135 | |
| 136 | == I have a `DateField` or `DateTimeField` with a default value of `now()`, but it's not working! == |
| 137 | |
| 138 | What you've probably done is something like this: |
| 139 | |
| 140 | {{{ |
| 141 | some_date_field = models.DateTimeField(default=datetime.datetime.now()) |
| 142 | }}} |
| 143 | |
| 144 | When you do that you're immediately calling `datetime.datetime.now` and passing its return value -- ''at the moment the class is defined'' -- to be the default, which means it will not change during the life of a server process. What you want to do instead is this: |
| 145 | |
| 146 | {{{ |
| 147 | some_date_field = models.DateTimeField(default=datetime.datetime.now) |
| 148 | }}} |
| 149 | |
| 150 | Note that there are no parentheses on `datetime.datetime.now` in this version, so you're passing ''the function itself'' to be the default. When Django receives a function as a default value for a model field, it will call the function each time a new object is saved, and that will correctly generate the current date/time for each object. |