Skip to main content

Deprecate django fields and make migrations without breaking existing code.

Project description

django-deprecation

Deprecate django fields and make migrations without breaking existing code.

Install

pip install django-deprecation

Usage

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-deprecation-0.1.0.tar.gz (3.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_deprecation-0.1.0-py2.py3-none-any.whl (3.7 kB view details)

Uploaded Python 2Python 3

File details

Details for the file django-deprecation-0.1.0.tar.gz.

File metadata

  • Download URL: django-deprecation-0.1.0.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for django-deprecation-0.1.0.tar.gz
Algorithm Hash digest
SHA256 0ae3cc4ed4164a6a7e425763c782b9175f689fcd146dd99fa9453ebaf1e34fa2
MD5 4767418d9f587f900f5036fef85ff9cb
BLAKE2b-256 31fd61d8d2329a10d1108e98ad5bb389ff9c6df0aca70eb1f62f41fb07ea901b

See more details on using hashes here.

File details

Details for the file django_deprecation-0.1.0-py2.py3-none-any.whl.

File metadata

  • Download URL: django_deprecation-0.1.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 3.7 kB
  • Tags: Python 2, Python 3
  • Uploaded using 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

File hashes

Hashes for django_deprecation-0.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 089455e7a6a6fa34884222e9746adb8e875c11d3b7aa0a88c0f7e99dbd44484a
MD5 9d378fd5c68315e8e8c0021a67b175cc
BLAKE2b-256 8611baa0de99d09ec24b85283bb42a3387fe49fc28d3f796db9df2118e39aa0b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page