---
title: "django-deprecation | pypx"
description: "Deprecate django fields and make migrations without breaking existing code."
canonical_url: "https://pypx.app/packages/django-deprecation"
meta:
  "og:description": "Deprecate django fields and make migrations without breaking existing code."
  "og:image": "https://pypx.app/_og/d/h_630%2Cc_PackageCard%2Cname_django-deprecation%2Cversion_0.1.1%2Csummary_Deprecate%2Bdjango%2Bfields%2Band%2Bmake%2Bmigrations%2Bwithout%2Bbreaking%2Bexisting%2Bcode.%2Clicense_MIT%2BLicense%2Cp_Ii9wYWNrYWdlcy9kamFuZ28tZGVwcmVjYXRpb24i%2Cs_zEwZbeX2_SaJtjw4.png"
  "og:image:height": "630"
  "og:image:type": image/png
  "og:image:width": "1200"
  "og:title": "django-deprecation 0.1.1"
  "twitter:image": "https://pypx.app/_og/d/h_630%2Cc_PackageCard%2Cname_django-deprecation%2Cversion_0.1.1%2Csummary_Deprecate%2Bdjango%2Bfields%2Band%2Bmake%2Bmigrations%2Bwithout%2Bbreaking%2Bexisting%2Bcode.%2Clicense_MIT%2BLicense%2Cp_Ii9wYWNrYWdlcy9kamFuZ28tZGVwcmVjYXRpb24i%2Cs_zEwZbeX2_SaJtjw4.png"
---

> Index of every page on this site: [llms.txt](https://pypx.app/llms.txt)

# **django-deprecation**

v0.1.1

Deprecate django fields and make migrations without breaking existing code.

4 KBpy2.py3-none-anyMIT License Likely Unmaintained

[** Diff **](https://pypx.app/packages/django-deprecation/diff)

`$ uv add django-deprecation`

# django-deprecation

[![build status](https://img.shields.io/travis/openbox/django-deprecation.svg)](https://travis-ci.org/openbox/django-deprecation) [![coverage](https://img.shields.io/codecov/c/github/openbox/django-deprecation.svg)](https://codecov.io/gh/openbox/django-deprecation) [![PyPI version](https://img.shields.io/pypi/v/django-deprecation.svg)](https://pypi.org/project/django-deprecation/) ![python version](https://img.shields.io/pypi/pyversions/django-deprecation.svg) ![django version](https://img.shields.io/pypi/djversions/django-deprecation.svg)

Deprecate django fields and make migrations without breaking existing code.

## Install

```
pip install django-deprecation
```

## Usage

### TL;DR

```
# Before:
class Album(models.Model):
    name = models.CharField(max_length=50)


# After:
class Album(models.Model):
    name = DeprecatedField('title')
    title = models.CharField(max_length=50)


assert album.name == album.title
assert list(Album.objects.filter(name='foo')) == list(Album.objects.filter(title='foo'))
```

### Long explanation

Let's suppose we have the following models:

```
from django.db import models


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


class Album(models.Model):
    musician = models.ForeignKey(Musician, on_delete=models.CASCADE)
    name = models.CharField(max_length=100)
```

Now, for some reason, let's suppose we want to rename the field `Album#musician` to `Album#artist`.

So we make the migration using the [RenameField](https://docs.djangoproject.com/en/1.11/ref/migration-operations/#renamefield) operation. The problem is that any existing code that used the old field would break.

We could create a property as an alias:

```
class Album(models.Model):
    artist = models.ForeignKey(Musician, on_delete=models.CASCADE)
    name = models.CharField(max_length=100)

    @property
    def musician(self):
        return self.artist

    @musician.setter
    def musician(self, value):
        self.artist = value
```

But any code using [QuerySet#filter](https://docs.djangoproject.com/en/2.0/ref/models/querysets/#filter) would break if it uses the `musician` field.

This is where `django-deprecation` comes handy. We set the `musician` field as a `DeprecatedField` and point it to the `artist` field:

```
from django_deprecation import DeprecatedField


class Album(models.Model):
    artist = models.ForeignKey(Musician, on_delete=models.CASCADE)
    musician = DeprecatedField('artist')
    name = models.CharField(max_length=100)
```

Now, the following code snippet will work:

```
from .models import Album, Musician

album = Album.objects.first()
assert album.musician == album.artist

new_musician = Musician.objects.create(
    first_name='John',
    last_name='Doe',
    instrument='Guitar',
)
album.musician = new_musician
assert album.artist == new_musician

new_musician_album = Album.objects.filter(
    musician=new_musician,
).first()
new_artist_album = Album.objects.filter(
    artist=new_musician,
).first()
assert new_musician_album == new_artist_album
```

If you want to control how to report the error, replace the `DeprecatedField.warn` function with a custom one:

```
from django_deprecation import DeprecatedField


def warn_function(message):
    # do stuff
    import warnings
    warnings.warn(message, DeprecationWarning)


DeprecatedField.warn = warn_function
```

## **Details **

<dl>

<dt>Version</dt>
<dd>0.1.1</dd>

<dt>License</dt>
<dd>MIT License</dd>

<dt>Maintainer</dt>
<dd>Tomás Fox</dd></dl>

## **Links**

- [Homepage](https://github.com/openbox/django-deprecation)
- [View on PyPI](https://pypi.org/project/django-deprecation/)

## **Maintainers **

- Tomás Fox · [tomas.c.fox@gmail.com](mailto:tomas.c.fox@gmail.com)

```json
{"@context":"https://schema.org","@graph":[{"@id":"https://pypx.app/#website","@type":"WebSite","description":"The Python Package Index, reimagined. Fast search, dependency insights, download trends, and changelogs — all in one place.","name":"pypx","url":"https://pypx.app/"},{"@id":"https://pypx.app/packages/django-deprecation#webpage","@type":"WebPage","description":"Deprecate django fields and make migrations without breaking existing code.","name":"django-deprecation","url":"https://pypx.app/packages/django-deprecation","isPartOf":{"@id":"https://pypx.app/#website"},"potentialAction":[{"@type":"ReadAction","target":["https://pypx.app/packages/django-deprecation"]}]},{"@id":"https://pypx.app/packages/django-deprecation#/schema/software-application/1","@type":"SoftwareApplication","applicationCategory":"DeveloperApplication","description":"Deprecate django fields and make migrations without breaking existing code.","license":"MIT License","name":"django-deprecation","softwareVersion":"0.1.1"}]}
```

## Sitemap

See the full [sitemap](https://pypx.app/sitemap.md) for all pages.
