Django Deprecate Fields

0.2.3 · active · verified Sat Apr 11

This package allows deprecating Django model fields and enables their removal in a backwards-compatible manner. It's particularly useful for maintaining migration consistency during rolling deployments where multiple application versions might run in parallel. The current version is 0.2.3, and releases are driven by changes merged to the main branch.

Warnings

Install

Imports

Quickstart

To deprecate a model field, import `deprecate_field` and wrap the existing field definition with it. After this, run `makemigrations`. The package automatically handles making the field nullable. The `return_instead` argument can be used to specify a value or callable to return when the deprecated field is accessed.

from django.db import models
from django_deprecate_fields import deprecate_field

class MyModel(models.Model):
    # Original field
    old_field = models.CharField(max_length=255)

    # To deprecate old_field, wrap it with deprecate_field
    # Run './manage.py makemigrations' after this change.
    deprecated_field = deprecate_field(models.CharField(max_length=255, null=True, blank=True), return_instead='default_value_or_none')
    # After deprecation and deployment, old_field can be safely removed.
    # deprecated_field = models.CharField(max_length=255) # This is how it would look after the first step
    # Then, after successful deployment, it can be entirely removed and a new migration created.

view raw JSON →