Add specific date as default datetime to DateTimeField in Django

Viewed 227

I have a model which already has multiple records. I now need to add a DateTimeField while giving a specific date as default for all the existing records of the model

in my model, I'm trying to do:

import datetime

class some_model:
    **some fields here**
    setup_date = models.DateTimeField(default = datetime.datetime(2020,1,1))

on running makemigrations and migrate I do not get any error but the field does not get added to the database

I've also tried with a callback function as follows

def default_datetime_for_my_class():
    return datetime.datetime(1900,1,1)

class some_model:
    **some fields here**
    setup_date = models.DateTimeField(default = default_datetime_for_my_class)
1 Answers

Did you include the app under the 'INSTALLED_APPS' field in 'settings.py'?. Also, make sure that you're able to access it from the admin page. Go in yourApp/admin.py and write

admin.site.register(yourModel)
Related