Skip to main content

This django Migration Operation can be used to transfer a fields default value to the database scheme.

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-add-default-value-0.9.1.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

django_add_default_value-0.9.1-py3-none-any.whl (11.0 kB view details)

Uploaded Python 3

File details

Details for the file django-add-default-value-0.9.1.tar.gz.

File metadata

  • Download URL: django-add-default-value-0.9.1.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for django-add-default-value-0.9.1.tar.gz
Algorithm Hash digest
SHA256 892b6b9a499173706034dc3cf396492be9e7de436d25252218d7b435ef8d090a
MD5 35663c8a9620ef72f62412187fd8a81a
BLAKE2b-256 552704b2a40ab414d7e6ef18323b94eb7077d18f62bff77660930d67fea4ceff

See more details on using hashes here.

File details

Details for the file django_add_default_value-0.9.1-py3-none-any.whl.

File metadata

  • Download URL: django_add_default_value-0.9.1-py3-none-any.whl
  • Upload date:
  • Size: 11.0 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for django_add_default_value-0.9.1-py3-none-any.whl
Algorithm Hash digest
SHA256 622393edff54bba2d0a02c3e0360763543d7ffcd63b8ef1c5e567ee52df1cd72
MD5 5f59acf3299d412e61dc59f7e70414ba
BLAKE2b-256 dd597aa85b35f36b08f3cc5eb2e1b3e667896370977a8d7a17f8f7e90610c558

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page