Pickled object field for Django

3.4.0 · active · verified Sat Apr 11

django-picklefield provides an implementation of a pickled object field for Django models. It enables storing any picklable Python object directly in a database field, handling automatic serialization and deserialization. The library is currently at version 3.4.0 and maintains a healthy release cadence, with updates typically occurring at least once a year to support newer Django and Python versions.

Warnings

Install

Imports

Quickstart

Define a model with `PickledObjectField` to store any picklable Python object. The `compress=True` argument can be used to enable zlib compression for larger objects. Data is automatically serialized upon saving and deserialized upon retrieval. The provided code demonstrates model definition and illustrates how to use the field to store complex data, including custom class instances.

import os
from django.db import models
from picklefield.fields import PickledObjectField

# Configure Django for a minimal setup (usually done in settings.py)
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')
# This is a dummy settings file for demonstration. In a real project,
# ensure 'picklefield' is in INSTALLED_APPS.
# A real settings.py would look something like:
# INSTALLED_APPS = ['django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'picklefield', 'myapp']
# DATABASES = {'default': {'ENGINE': 'django.db.backends.sqlite3', 'NAME': ':memory:'}}
# SECRET_KEY = 'insecure-dev-key'

class MyModel(models.Model):
    data = PickledObjectField(compress=True)

    def __str__(self):
        return f"MyModel with data: {self.data}"

# Example Usage (assuming Django setup is complete, e.g., via manage.py shell)
if __name__ == '__main__':
    # This part would typically be run within a Django shell or application context
    # For a standalone runnable example, mocking Django ORM is complex.
    # The following illustrates usage, but won't run directly without a full Django setup.
    
    # Create a dummy class to demonstrate pickling custom objects
    class CustomObject:
        def __init__(self, name, value):
            self.name = name
            self.value = value
        def __repr__(self):
            return f"CustomObject(name='{self.name}', value={self.value})"

    print("To run this, ensure Django is configured and 'picklefield' is in INSTALLED_APPS.")
    print("Then run `python manage.py shell` and execute the following:")
    print("  from myapp.models import MyModel")
    print("  from myapp.quickstart import CustomObject") # assuming this code is in myapp/quickstart.py
    print("  obj = MyModel()
  obj.data = {'list': [1, 2, {'a': 3}], 'custom': CustomObject('test', 123)}
  obj.save()
  retrieved_obj = MyModel.objects.first()
  print(f'Stored data: {retrieved_obj.data}')
  print(f'Type of retrieved data: {type(retrieved_obj.data)}')
  print(f'Type of custom object: {type(retrieved_obj.data["custom"])}')")

view raw JSON →