Skip to main content

Django Add Default Value

Django Migration Operation that can be used to transfer a field's default value to the database scheme.

PyPi License Contributing 3yourminD-Careers Stars

Supported Databases

  • MySQL (or compatible)
  • PostgreSQL
  • MSSQL (currently not tested)
  • CockroachDB

Installation

pip install django-add-default-value

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 a autogenerated Migration, that adds a new field

AddDefaultValue(
    model_name='my_model',
    name='my_field',
    value='my_default'
)

Example

Given the following migration:

BEFORE

from django.db import migrations, models


class Migration(migrations.Migration):
    dependencies = [
        ...
    ]

    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:

AFTER

from django.db import migrations, models

from django_add_default_value import AddDefaultValue


class Migration(migrations.Migration):
    dependencies = [
        ...
    ]

    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.

Contributing

First of all, thank you very much for contributing to this project. Please base your work on the master branch and target master in your pull request.

To succesfully use the dbshell management command (very useful for debugging), the client binaries for the respective database engines are needed.

Then install pipenv. Edit the Pipfile to select your Django version and the accompanying MS-SQL driver. Make sure you don't commit this change in any pull request - we always set it to the oldest supported version.

Once you've updated the Pipfile, run pipenv install --python 3 --dev. You should now have a working development environment as a virtualenv. To access it, run pipenv shell or prefix commands with pipenv run. For more information see the pipenv documentation.

Testing

Copy the relevant sample settings file in test_project to the file without .sample in it. Adjust the values to match your environment (or match your environment to the values).

You should now be able to run the tests using tox. Select your environment when needed, using the -e command line flag. See Tox's excellent documentation.

License

django-add-default-value is released under the Apache 2.0 License.

Release files for django-add-default-value 0.10.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-add-default-value 0.10.0
File Size Uploaded
django-add-default-value-0.10.0.tar.gz 7.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-add-default-value 0.10.0
File Interpreter ABI Platform
django_add_default_value-0.10.0-py3-none-any.whl Python 3 none any Details

Total release size: 18.9 kB

Release files / django-add-default-value-0.10.0.tar.gz

Download URL django-add-default-value-0.10.0.tar.gz
Size 7.8 kB
Tags Source
SHA-256 checksum
How to use checksums
a539767f498ab2e3022f98f743aa4473354a24df037859543cd2545f650cadcc
BLAKE2b-256 checksum
How to use checksums
50534ef8e3920cdc6f098ea73635ba9503b69116cff218deba195453065e2ace
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.6.1 requests/2.25.0 setuptools/51.1.1 requests-toolbelt/0.9.1 tqdm/4.55.1 CPython/3.8.6

Release files / django_add_default_value-0.10.0-py3-none-any.whl

Download URL django_add_default_value-0.10.0-py3-none-any.whl
Size 11.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
514b357f4a5e5c5dd9f02ca42d19d51688ed4a9cac62a7e35d09025f632bba93
BLAKE2b-256 checksum
How to use checksums
a699ade1da8c7e4087ea603adaf89021e7ba86685c26c54f1429093bb9a00776
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.6.1 requests/2.25.0 setuptools/51.1.1 requests-toolbelt/0.9.1 tqdm/4.55.1 CPython/3.8.6

Release history Release notifications | RSS feed

This release

0.10.0 This release

2 release files

0.9.1

2 release files

0.9.0

2 release files

0.8.0

2 release files

0.7.0

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.0.8

1 release file

0.0.7

2 release files

0.0.6

1 release file

0.0.5

1 release file

0.0.4

1 release file

0.0.3

1 release file

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page