> I'm baffled by the fact that the __str__ method of an instance fails
> before the instance is saved but works fine afterwards.
>
> The relevant snippet from my models.py file:
> class Journal(models.Model):
> date = models.DateTimeField(default=timezone.now)
Check the docs on the use of 'default'. The value or callable in this case is used when the object is created by saving the object instance to the database. Granted, the wording should probably be more specific since there is an overlap of meanings for creating the object.
https://docs.djangoproject.com/en/1.8/ref/models/fields/#default
It doesn't particularly make sense to do what you are doing in the first place. You are attributing a time stamp to a volatile piece of data in memory, and in reality it is already gone from memory by the time the user sees it since the response cycle has ended, and is likely being dynamically rebuilt upon the next request cycle in response to form data. What if the user takes 30 minutes to decide to click the submit button and finalize saving the object to the database?
Which moment of time would you want to capture: a) the time the object is created in memory and displayed to the user or b) the moment the object is saved to a non-volatile location like the DB? Most times you'll want the latter since that's when an actual change occurred.
However, if you truly want to catch the moment that the object instance was created in memory, you'll need to add date=timezone.now to your Journal() call instead of relying on the default value of the model field, which only is populated at the point where save() is called and no previous value exists. Obviously if this is part of a confirmation form, you'll need to pass the original timestamp along through the form as well, or you'll wind up with option b) above anyway.
-James