{"id":7161,"library":"django-add-default-value","title":"Django Add Default Value","description":"django-add-default-value is a Django Migration Operation that facilitates transferring a field's default value directly to the database schema. This addresses Django's default behavior of handling `default` values at the application level and dropping the database `DEFAULT` constraint after initial row population. The library is currently active, with version 0.10.0 released in February 2022, and helps ensure database-level default constraints persist for improved data integrity and compatibility with non-Django database access.","status":"active","version":"0.10.0","language":"en","source_language":"en","source_url":"https://github.com/3YOURMIND/django-add-default-value","tags":["django","migrations","database","default-value","schema","data-integrity"],"install":[{"cmd":"pip install django-add-default-value","lang":"bash","label":"Install with pip"}],"dependencies":[{"reason":"Core framework dependency for migration operations.","package":"Django"},{"reason":"Explicitly supported database backend.","package":"MySQL","optional":true},{"reason":"Explicitly supported database backend.","package":"PostgreSQL","optional":true},{"reason":"Explicitly supported database backend (currently not tested by library maintainers).","package":"MSSQL","optional":true},{"reason":"Explicitly supported database backend.","package":"CockroachDB","optional":true}],"imports":[{"symbol":"AddDefaultValue","correct":"from django_add_default_value import AddDefaultValue"}],"quickstart":{"code":"import os\nimport django\nfrom django.conf import settings\nfrom django.db import migrations, models\nfrom django_add_default_value import AddDefaultValue\n\n# Minimal Django setup for demonstration\nif not settings.configured:\n    settings.configure(\n        INSTALLED_APPS=['my_app'],\n        DATABASES={'default': {'ENGINE': 'django.db.backends.sqlite3', 'NAME': ':memory:'}}\n    )\n    django.setup()\n\n# Example of a migration file using AddDefaultValue\nclass Migration(migrations.Migration):\n\n    dependencies = [\n        # ... other app dependencies\n    ]\n\n    operations = [\n        migrations.AddField(\n            model_name='my_model',\n            name='my_field',\n            field=models.CharField(default='my_default_value', max_length=255),\n        ),\n        AddDefaultValue(\n            model_name='my_model',\n            name='my_field',\n            value='my_default_value'\n        )\n    ]","lang":"python","description":"To use `django-add-default-value`, manually add the `AddDefaultValue` operation to an autogenerated migration file immediately after an `AddField` operation. This ensures that the specified default value for the field is transferred to the database schema, maintaining the default constraint at the database level."},"warnings":[{"fix":"Use `django-add-default-value.AddDefaultValue` in your migrations for critical fields to ensure the database schema itself enforces the default, or leverage Django's `db_default` option (available in Django 4.2+).","message":"Django's default migration behavior does not typically set persistent database-level DEFAULT constraints for model fields. It applies a one-off default to existing rows and then drops the database constraint, relying on the application to enforce defaults for new records. This can lead to inconsistencies or issues with non-Django database interactions.","severity":"gotcha","affected_versions":"All Django versions (without explicit `db_default` or similar)"},{"fix":"To establish a persistent database default, either add `default='some_value'` to your model field and then use `AddDefaultValue` in the migration, or, for simple static defaults, define `db_default` directly on the field (Django 4.2+).","message":"When adding a new non-nullable field to an existing model without explicitly providing a `default` in `models.py`, `makemigrations` will prompt you to provide a 'one-off' default. This default is temporary and only used to populate existing rows, not to establish a persistent database-level default.","severity":"breaking","affected_versions":"All Django versions"},{"fix":"For callable defaults, `django-add-default-value` might not be the direct solution for dynamic database defaults (as SQL defaults are often static). Consider if a database-level default is truly required, or if application-level enforcement is sufficient. If a static default based on the callable's *initial* value is acceptable for the database, `AddDefaultValue` can be used with that static value.","message":"Using callable functions as `default` values in Django models (e.g., `default=uuid.uuid4`). Django executes the callable once during migration to populate existing rows and then drops the database default, relying on Python to call the function for new object creation. This means the database schema itself won't have a dynamic default constraint.","severity":"gotcha","affected_versions":"All Django versions"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Edit your generated migration file to include `AddDefaultValue(model_name='...', name='...', value='...')` after `migrations.AddField`. Alternatively, add `default='some_value'` to the field definition in `models.py` before `makemigrations` and then include `AddDefaultValue`.","cause":"Attempting to add a new non-nullable field to a model that already contains data, without providing a default value in `models.py` or through the migration prompt. Django needs a value to populate existing rows.","error":"It is impossible to add a non-nullable field '...' to ... without specifying a default."},{"fix":"Ensure `AddDefaultValue` is used in the migration that adds the non-nullable field. This establishes a `DEFAULT` constraint at the database level, preventing `NOT NULL` violations when no value is explicitly provided. Run `python manage.py migrate` to apply the migration.","cause":"A new non-nullable field was added without a persistent database-level default. When new records are inserted (e.g., directly into the database, or by a non-Django process), and no value is provided for 'my_field', the database's NOT NULL constraint is violated.","error":"django.db.utils.IntegrityError: NOT NULL constraint failed: my_app_my_model.my_field"},{"fix":"Run `pip install django-add-default-value` to install the library. Verify the import statement is `from django_add_default_value import AddDefaultValue`.","cause":"The `django-add-default-value` package is not installed in the active Python environment, or there's a typo in the import statement.","error":"ModuleNotFoundError: No module named 'django_add_default_value'"}]}