Add 'auto_now' DateTimeField to existing Django model

Is it possible to add to existing fields auto_nowand auto_now_addDateTime>

class ExistingModel(models.Model):
    # ... model stuff
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

We cannot add these fields without default, but adding a value defaultgives an error:

./manage.py makemigrations

returns:

(fields.E160) The options auto_now, auto_now_add, and default are mutually exclusive. Only one of these options may be present.
+4
source share
1 answer

You can do this in two separate transitions.

First add default=<some datetime>datetime to both of your new fields and create this hyphenation.

Then delete the parameters defaultand add auto_nowand create a second migration.

+6
source

Source: https://habr.com/ru/post/1693734/


All Articles