Adding a new field into the existing table without giving a default value to it [Django]

just out of curiosity is there any way to add a new field to the Django model without giving a default value?

Like I have a model and a new field needed but the field can't be null or blank and the default value is not acceptable

You can recreate completely the model and table if you are in development process (delete migrations of the app and recall makemigrations and migrate django command) but after this step, it is not possible without data loss

On the other hand, you can use a function to define the default value of a field of a model. This may help you to determine the default value at creation time?

def get_default_value():
    return "value"

message = CharField(default=get_default_value)

More informations: https://docs.djangoproject.com/en/dev/ref/models/fields/#default

Back to Top