Unexpected behaviour with db_default and auto_now_add

When creating a datetime field with both auto_now_add=True and db_default=Now(), I noticed something which may be a bug.

When setting both a default and a db_default (according to the docs and in practice), the default takes precedence when creating a record without saving it to the db. However, although auto_now_add takes the place of using the default parameter, when it is used, it does not take precedence, and the resulting record has the field set to models.expressions.DatabaseDefault.

Is this expected behaviour, or should I file a bug report?

I’m curious, how are you identifying this as an issue?

It would seem to me that you’re going to be getting the same value either way. (Or close enough that you’re not going to be able to tell where the value is coming from.)

Thanks for the response.

Your question made me realise that a field with auto_now_add only sets the datetime when it is saved to the db. I was writing a test using a temp record that hadn’t been saved to the db, and expecting it to have a datetime that I could inspect. I now see that if I remove the db_default param then the field is a NoneType until it is saved to the db.

While technically the behaviour does still change if you set db_default=Now() (The field is models.expressions.DatabaseDefault instead of NoneType), this is a much less meaningful difference than my original misconception.