django-deprecation
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 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
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
Metadata
Release files for django-deprecation 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| django-deprecation-0.1.1.tar.gz | 4.0 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_deprecation-0.1.1-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details |
Total release size: 7.8 kB
Release files / django-deprecation-0.1.1.tar.gz
| Download URL | django-deprecation-0.1.1.tar.gz |
|---|---|
| Size | 4.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6dfe3f91914947ba8bb2eb58b4fe26fcac710301c3f533439f4362f5116f6e57
|
|
BLAKE2b-256 checksum How to use checksums |
4a53aeb158bd41298b3699a8019535aff4c90063250a95ccfd888c59ae176ee0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.5.2
|
Release files / django_deprecation-0.1.1-py2.py3-none-any.whl
| Download URL | django_deprecation-0.1.1-py2.py3-none-any.whl |
|---|---|
| Size | 3.9 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
9be8f772c0e7b7acbf1646bea6fc2242ba36404a42bd46b29e812ff3dfd8acc4
|
|
BLAKE2b-256 checksum How to use checksums |
bfa942c43bc1ceecdeccb0954a245b26bf987e2bd91c110b34557b89dcdc495f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.5.2
|
Security for django-deprecation 0.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.