https://github.com/xplor/django-add-default-value-postgresql
Add default field value in Django migrations for PostgreSQL
https://github.com/xplor/django-add-default-value-postgresql
django postgresql
Last synced: 5 months ago
JSON representation
Add default field value in Django migrations for PostgreSQL
- Host: GitHub
- URL: https://github.com/xplor/django-add-default-value-postgresql
- Owner: xplor
- License: apache-2.0
- Created: 2020-03-04T16:55:22.000Z (over 6 years ago)
- Default Branch: main
- Last Pushed: 2025-04-07T19:48:16.000Z (about 1 year ago)
- Last Synced: 2025-08-28T05:18:44.263Z (10 months ago)
- Topics: django, postgresql
- Language: Python
- Homepage:
- Size: 43.9 KB
- Stars: 5
- Watchers: 23
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Django Add Default Value for PostgreSQL
Django Migration Operation that can be used to transfer a field's default value
to the database scheme.
Based on [django-add-default-value](https://github.com/3YOURMIND/django-add-default-value/) by [3YOURMIND](https://github.com/3YOURMIND). This variant drops support for other databases, specifically the Azure ODBC driver which does not support current versions of Django.
[](https://github.com/Mariana-Tek/django-add-default-value-postgresql/actions?query=workflow%3A%22Python+package%22)
[](https://pypi.org/project/django-add-default-value-postgresql/)
## Supported Versions of Python and Django
- Python 3.10, 3.11, and 3.12
- Django 4.0, 4.1, 4.2, 5.0, 5.1, and 5.2
## Installation
`uv add django-add-default-value-postgresql`
You can then use `AddDefaultValue` in your migration file to transfer the default
values to your database. Afterwards, it's just the usual `./manage.py migrate`.
## Usage
Add this manually to an autogenerated Migration that adds a new field:
AddDefaultValue(
model_name='my_model',
name='my_field',
value='my_default'
)
### Example
Given the following migration:
operations = [
migrations.AddField(
field=models.CharField(default='my_default', max_length=255),
model_name='my_model',
name='my_field',
),
]
Modify the migration to add a default value:
+from django_add_default_value import AddDefaultValue
+
operations = [
migrations.AddField(
field=models.CharField(default='my_default', max_length=255),
model_name='my_model',
name='my_field',
),
+ AddDefaultValue(
+ model_name='my_model',
+ name='my_field',
+ value='my_default'
+ )
]
If you check `python manage.py sqlmigrate [app name] [migration]`,
you will see that the default value now gets set.
## Testing
You can test against multiple versions of Django using `tox`. To test across Python versions, use pyenv to run `tox` under each version.
## License
django-add-default-value-postgresql is released under the Apache 2.0 License, based on [django-add-default-value](https://github.com/3YOURMIND/django-add-default-value/) by [3YOURMIND](https://github.com/3YOURMIND) which is also released under the Apache 2.0 License.
### Changes from original source
- removed MySQL-related code
- removed MSSQL-related code
- added allow_migrate_model check on database_forwards and database_backwards
- added support for Python 3.10, 3.11, and 3.12, dropped support for <3.10
- added support for Django 4.0, 4.1, 4.2, 5.0, 5.1, and 5.2, dropped support for <4.0