• Stars
    star
    129
  • Rank 279,262 (Top 6 %)
  • Language
    Python
  • License
    Apache License 2.0
  • Created almost 7 years ago
  • Updated almost 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

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.

More Repositories

1

django-migration-linter

πŸš€ Detect backward incompatible migrations for your django project
Python
507
star
2

django-deprecate-fields

This package allows deprecating model fields and allows removing them in a backwards compatible manner.
Python
152
star
3

kotti

πŸ’… 3YOURMIND’s Vue UI Framework & Design System
TypeScript
77
star
4

django-replace-migrations

Python
12
star
5

fire-pr

πŸ”₯ Create Pull Requests in an approachable way, this extension will streamline your pull request proces
Vue
9
star
6

js-polyfill-docker

🐳Docker container for the polyfill.io service to serve JavaScript bundles per browser efficiently.
Dockerfile
7
star
7

vue-comments

πŸ“ A Vue.js plugin to show comments related to an identifier. This identifier is called projectId internally but since this component is written with a dynamic mindset you can provide your own actions to deal with CRUD operations on comments.
JavaScript
6
star
8

drf-payload-customizer

This package allows you to customize your django-rest-framework serializer i/o in specific ways.
Python
5
star
9

3YDMoeller

A C++ single header implementation of Moeller's Triangle-Triangle & Triangle-Box intersection algorithms for BVH trasversal
C++
5
star
10

yoco

πŸ’Ž Icons used in the applications of 3YOURMIND. MOVED TO THE KOTTI REPOSITORY.
3
star
11

vue-yodify

πŸ’¬ A Vue.js notification plugin with easy installation and usage
Vue
3
star
12

django-model-sync

Python
2
star
13

xfvb-run

fork of a gist
Shell
1
star
14

vscode-frontend-extension-pack

The repository for the 3yourmind Frontend Extension Pack
JavaScript
1
star