### Installing django-cryptography Library (Console) Source: https://github.com/georgemarshall/django-cryptography/blob/master/README.rst Provides the command-line instruction to install the django-cryptography library using pip, the Python package installer. This command fetches the library and its dependencies from PyPI. ```console pip install django-cryptography ``` -------------------------------- ### Install django-cryptography via pip Source: https://github.com/georgemarshall/django-cryptography/blob/master/docs/installation.rst This command uses the pip package installer to download and install the django-cryptography library from the Python Package Index (PyPI). Ensure you have Python and pip installed and configured correctly before running this command. ```console pip install django-cryptography ``` -------------------------------- ### Defining Model for Encryption Migration - Django/Python Source: https://github.com/georgemarshall/django-cryptography/blob/master/docs/migrating.rst Defines the initial Django model structure with a standard CharField that will later be migrated to an encrypted field. This is the starting point before the migration process begins. ```python class EncryptedCharModel(models.Model): field = encrypt(models.CharField(max_length=15)) ``` -------------------------------- ### Defining Encrypted Field Django Python Source: https://github.com/georgemarshall/django-cryptography/blob/master/docs/examples.rst This snippet defines a Django model `MyModel` with a standard `CharField` and another `CharField` wrapped with the `encrypt` function from `django_cryptography.fields`. This configuration ensures that data saved to the `sensitive_data` field is automatically encrypted before being stored in the database and decrypted when retrieved. ```python from django.db import models from django_cryptography.fields import encrypt class MyModel(models.Model): name = models.CharField(max_length=50) sensitive_data = encrypt(models.CharField(max_length=50)) ``` -------------------------------- ### Configure Django Cryptography Signing Backend (Python) Source: https://github.com/georgemarshall/django-cryptography/blob/master/docs/settings.rst This snippet demonstrates how to configure the SIGNING_BACKEND setting in Django to use the TimestampSigner provided by django-cryptography, which leverages the cryptography.io library for signing operations. ```python SIGNING_BACKEND = 'django_cryptography.core.signing.TimestampSigner' ``` -------------------------------- ### Creating Initial Model Migration - Django/Python Source: https://github.com/georgemarshall/django-cryptography/blob/master/docs/migrating.rst Generates the first Django migration to create the EncryptedCharModel with the original, unencrypted 'field'. This migration establishes the initial state of the database schema. ```python class Migration(migrations.Migration): initial = True dependencies = [] operations = [ migrations.CreateModel( name='EncryptedCharModel', fields=[ ('id', models.AutoField( auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('field', models.CharField(max_length=15)), ], ), ] ``` -------------------------------- ### Copying Data with RunPython Migration - Django/Python Source: https://github.com/georgemarshall/django-cryptography/blob/master/docs/migrating.rst This Django data migration uses `RunPython` to execute custom Python functions. The `forwards` function copies data from the old unencrypted field ('old_field') to the new encrypted field ('field'), while the `reverse` function allows rolling back by copying data from the new field back to the old one. ```python def forwards_encrypted_char(apps, schema_editor): EncryptedCharModel = apps.get_model("fields", "EncryptedCharModel") for row in EncryptedCharModel.objects.all(): row.field = row.old_field row.save(update_fields=["field"]) def reverse_encrypted_char(apps, schema_editor): EncryptedCharModel = apps.get_model("fields", "EncryptedCharModel") for row in EncryptedCharModel.objects.all(): row.old_field = row.field row.save(update_fields=["old_field"]) class Migration(migrations.Migration): dependencies = [ ("fields", "0003_add_encrypted_fields"), ] operations = [ migrations.RunPython(forwards_encrypted_char, reverse_encrypted_char), ] ``` -------------------------------- ### Encrypting Django Model Field with django-cryptography (Python) Source: https://github.com/georgemarshall/django-cryptography/blob/master/README.rst Demonstrates how to use the `encrypt` field wrapper from `django_cryptography.fields` to automatically encrypt a specific model field when saving data to the database. This uses symmetrical encryption allowing for bi-directional data retrieval. ```python from django.db import models from django_cryptography.fields import encrypt class MyModel(models.Model): name = models.CharField(max_length=50) sensitive_data = encrypt(models.CharField(max_length=50)) ``` -------------------------------- ### Removing Old Field Migration - Django/Python Source: https://github.com/georgemarshall/django-cryptography/blob/master/docs/migrating.rst The final Django schema migration in the sequence. It removes the temporary 'old_field' now that all data has been successfully copied and encrypted in the new 'field'. ```python class Migration(migrations.Migration): dependencies = [ ('fields', '0004_migrate_data'), ] operations = [ migrations.RemoveField( model_name='encryptedcharmodel', name='old_field', ), ] ``` -------------------------------- ### Renaming Original Field Migration - Django/Python Source: https://github.com/georgemarshall/django-cryptography/blob/master/docs/migrating.rst A Django schema migration that renames the existing unencrypted 'field' to 'old_field'. This step preserves the original data while making way for the new encrypted field with the original name. ```python class Migration(migrations.Migration): dependencies = [ ('fields', '0001_initial'), ] operations = [ migrations.RenameField( model_name='encryptedcharmodel', old_name='field', new_name='old_field', ), ] ``` -------------------------------- ### Adding New Encrypted Field Migration - Django/Python Source: https://github.com/georgemarshall/django-cryptography/blob/master/docs/migrating.rst A Django schema migration that adds the new encrypted field, also named 'field', to the model. This field uses the `django_cryptography.fields.encrypt` wrapper. ```python class Migration(migrations.Migration): dependencies = [ ('fields', '0002_rename_fields'), ] operations = [ migrations.AddField( model_name='encryptedcharmodel', name='field', field=django_cryptography.fields.encrypt( models.CharField(default=None, max_length=15)), preserve_default=False, ), ] ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.