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.
pip install django-picklefieldVerified import paths — ran on the pinned version, not inferred.
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.
NEVER store user-controllable data directly in a `PickledObjectField`. For user-provided data, use secure serialization formats like JSON, or ensure strict validation and sanitization. If `picklefield` is used for internal, trusted data, ensure no untrusted input can influence the stored objects.
After retrieving data with `values()` or `values_list()`, manually decode and unpickle the string. For example, using the internal `dbsafe_decode` function if accessed, or simply `pickle.loads(base64.b64decode(value))` if `compress=False` was used.
If you need to store Django model instances, wrap them in a simple data structure like a list or tuple (e.g., `obj.data = [my_django_model_instance]`) before assigning them to the `PickledObjectField`.
Always check the `django-picklefield` release notes or `pyproject.toml` for explicit Python and Django version compatibility before upgrading. Ensure your project's Python and Django versions meet the requirements of the `django-picklefield` version you intend to use.
Plan for backward compatibility when modifying classes stored in `PickledObjectField`. Consider versioning your pickled objects, implementing custom `__setstate__` and `__getstate__` methods, or providing migration logic for old object structures if breaking changes are necessary.
Ensure `django-picklefield` is installed (`pip install django-picklefield`) and your model imports `PickledObjectField` from `picklefield.fields`: `from picklefield.fields import PickledObjectField`.
Modify the object being stored to exclude unpicklable components, ensure all parts are defined at the top level of a module, or convert complex objects like QuerySets into simpler, picklable data structures (e.g., lists of IDs or dictionaries) before saving. For Django models, store their primary keys rather than the model instances directly if possible.
Avoid moving or renaming classes whose instances are stored in `PickledObjectField`. If unavoidable, create a 'ghost' class at the old location that imports and re-exposes the moved class, or implement custom serialization logic to handle class migrations. For Python 2 to 3 migrations, ensure data is migrated correctly as pickle protocols can differ.
Refactor your queries to use only the supported lookup types: `exact`, `in`, or `isnull`. If more complex queries are needed, consider storing queryable attributes in separate, standard Django fields, or perform deserialization and filtering in Python code after retrieving objects.