Django Deprecation (Field Management)

0.1.1 · active · verified Sun Apr 12

django-deprecation is a Python library designed to facilitate the deprecation and renaming of Django model fields without introducing breaking changes to existing code or requiring immediate, complex database migrations. It helps maintain backward compatibility during phased rollouts, especially in environments with rolling deployments. The current version is 0.1.1, and its release cadence appears to be feature-driven rather than time-based.

Warnings

Install

Imports

Quickstart

This example demonstrates how to use `DeprecatedField` to rename a model field (`musician` to `artist`) while maintaining backward compatibility for existing code that still references the old field name. The `DeprecatedField` ensures that lookups and access via the old name are correctly redirected to the new field. This typically involves a multi-step migration process.

import os
import django
from django.db import models
from django_deprecation import DeprecatedField

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings') # Replace with your project settings
django.setup()

class Musician(models.Model):
    name = models.CharField(max_length=50)

    def __str__(self):
        return self.name

class Album(models.Model):
    artist = models.ForeignKey(Musician, on_delete=models.CASCADE)
    # Deprecate 'musician' field, pointing to 'artist'
    musician = DeprecatedField('artist')
    name = models.CharField(max_length=100)

    def __str__(self):
        return self.name

# Example usage (assuming models are migrated and data exists)
# No runnable example for DB operations without full Django setup
# print('Demonstrating DeprecatedField behavior:')
# band = Musician.objects.create(name='The Beatles')
# album = Album.objects.create(artist=band, name='Abbey Road')
# assert album.musician == album.artist
# print(f"Album name: {album.name}, Musician (deprecated): {album.musician.name}, Artist (new): {album.artist.name}")

view raw JSON →