Registry / database / django-add-default-value

django-add-default-value

JSON →
library0.10.0pypypi✓ verified 86d ago

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.

pip install django-add-default-value
INSTALL
IMPORT
SIG · DJANGO-ADD-DEFAULT
D
django-add-default-value
databasepythonv0.10.0
Install
3.4s avg
Import
668ms
Disk
65MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.10.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 0.704s · 66.3MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 3.4s · import 0.632s · 67MB
65MB installed
● package 65MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

AddDefaultValue
from django_add_default_value import AddDefaultValue

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.

import os import django from django.conf import settings from django.db import migrations, models from django_add_default_value import AddDefaultValue # Minimal Django setup for demonstration if not settings.configured: settings.configure( INSTALLED_APPS=['my_app'], DATABASES={'default': {'ENGINE': 'django.db.backends.sqlite3', 'NAME': ':memory:'}} ) django.setup() # Example of a migration file using AddDefaultValue class Migration(migrations.Migration): dependencies = [ # ... other app dependencies ] operations = [ migrations.AddField( model_name='my_model', name='my_field', field=models.CharField(default='my_default_value', max_length=255), ), AddDefaultValue( model_name='my_model', name='my_field', value='my_default_value' ) ]
Debug
Known issues
gotchaDjango'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.
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+).
affects: All Django versions (without explicit `db_default` or similar)
breakingWhen 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.
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+).
affects: All Django versions
gotchaUsing 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.
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.
affects: All Django versions
Errors
Common errors & fixes
It is impossible to add a non-nullable field '...' to ... without specifying a default.
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.
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`.
django.db.utils.IntegrityError: NOT NULL constraint failed: my_app_my_model.my_field
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.
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.
ModuleNotFoundError: No module named 'django_add_default_value'
The `django-add-default-value` package is not installed in the active Python environment, or there's a typo in the import statement.
fix
Run `pip install django-add-default-value` to install the library. Verify the import statement is `from django_add_default_value import AddDefaultValue`.
Upgrade
Version history
0.10.0latest on PyPI · released Feb 2, 2022
Audit
Dependencies
DjangorequiredCore framework dependency for migration operations.
MySQLoptionalExplicitly supported database backend.
PostgreSQLoptionalExplicitly supported database backend.
MSSQLoptionalExplicitly supported database backend (currently not tested by library maintainers).
CockroachDBoptionalExplicitly supported database backend.
Agent activity
13 hits · last 30 days
node
12
Resources
django-add-default-value — pip install django-add-default-value · libregistry