A pickled object field for Django

Overview

django-picklefield

License Latest Version Build Status Coverage Status Supported Python Versions Wheel Status

About

django-picklefield provides an implementation of a pickled object field. Such fields can contain any picklable objects.

The implementation is taken and adopted from Django snippet #1694 by Taavi Taijala, which is in turn based on Django snippet #513 by Oliver Beattie.

django-picklefield is available under the MIT license.

Usage

First of all, you need to have django-picklefield installed; for your convenience, recent versions should be available from PyPI.

To use, just define a field in your model:

>>> from picklefield.fields import PickledObjectField
... class SomeObject(models.Model):
...     args = PickledObjectField()

and assign whatever you like (as long as it's picklable) to the field:

>>> obj = SomeObject()
>>> obj.args = ['fancy', {'objects': 'inside'}]
>>> obj.save()

Notes

If you need to serialize an object with a PickledObjectField for transmission to the browser, you may need to subclass the field and override the value_to_string() method. Currently pickle fields are serialized as base64-encoded pickles, which allows reliable deserialization, but such a format is not convenient for parsing in the browser. By overriding value_to_string() you can choose a more convenient serialization format.

Fields now accept the boolean key word argument copy, which defaults to True. The copy is necessary for lookups to work correctly. If you don't care about performing lookups on the picklefield, you can set copy=False to save on some memory usage. This an be especially beneficial for very large object trees.

Running tests

The full test suite can be run with Tox:

>>> pip install tox
>>> tox

Original notes

Here are the notes by taavi223, the original author:

Incredibly useful for storing just about anything in the database (provided it is Pickle-able, of course) when there isn't a 'proper' field for the job.

PickledObjectField is database-agnostic, and should work with any database backend you can throw at it. You can pass in any Python object and it will automagically be converted behind the scenes. You never have to manually pickle or unpickle anything. Also works fine when querying; supports exact, in, and isnull lookups. It should be noted, however, that calling QuerySet.values() will only return the encoded data, not the original Python object.

This PickledObjectField has a few improvements over the one in snippet #513.

This one solves the DjangoUnicodeDecodeError problem when saving an object containing non-ASCII data by base64 encoding the pickled output stream. This ensures that all stored data is ASCII, eliminating the problem.

PickledObjectField will now optionally use zlib to compress (and uncompress) pickled objects on the fly. This can be set per-field using the keyword argument "compress=True". For most items this is probably not worth the small performance penalty, but for Models with larger objects, it can be a real space saver.

You can also now specify the pickle protocol per-field, using the protocol keyword argument. The default of 2 should always work, unless you are trying to access the data from outside of the Django ORM.

Worked around a rare issue when using the cPickle and performing lookups of complex data types. In short, cPickle would sometimes output different streams for the same object depending on how it was referenced. This of course could cause lookups for complex objects to fail, even when a matching object exists. See the docstrings and tests for more information.

You can now use the isnull lookup and have it function as expected. A consequence of this is that by default, PickledObjectField has null=True set (you can of course pass null=False if you want to change that). If null=False is set (the default for fields), then you wouldn't be able to store a Python None value, since None values aren't pickled or encoded (this in turn is what makes the isnull lookup possible).

You can now pass in an object as the default argument for the field without it being converted to a unicode string first. If you pass in a callable though, the field will still call it. It will not try to pickle and encode it.

You can manually import dbsafe_encode and dbsafe_decode from fields.py if you want to encode and decode objects yourself. This is mostly useful for decoding values returned from calling QuerySet.values(), which are still encoded strings.

Note: If you are trying to store other django models in the PickledObjectField, please see the comments for a discussion on the problems associated with doing that. The easy solution is to put django models into a list or tuple before assigning them to the PickledObjectField.

Update 9/2/09: Fixed the value_to_string method so that serialization should now work as expected. Also added deepcopy back into dbsafe_encode, fixing #4 above, since deepcopy had somehow managed to remove itself. This means that lookups should once again work as expected in all situations. Also made the field editable=False by default (which I swear I already did once before!) since it is never a good idea to have a PickledObjectField be user editable.

Changes

Changes in version 3.0.0

  • Allowed default pickle protocol to be overriden using the PICKLEFIELD_DEFAULT_PROTOCOL setting.
  • Dropped support for Python 2.
  • Added testing against Django 3.0.
  • Dropped support for Django 1.11.

Changes in version 2.1.0

  • Added official support for Django 2.2 (thanks to joehybird).
  • Dropped support for Django 2.0 and 2.1 (thanks to joehybird).
  • Dropped support for Python 3.4 (thanks to joehybidd).

Changes in version 2.0.0

  • Silenced RemovedInDjango30Warning warnings on Django 2.0+ (thanks to canarduck).
  • Restructured project directories.
  • Disallowed the usage of empty strings for PickledObjectField. That makes .save(), .create(), etc. raise IntegrityError if null is not True and no default value was specified like built-in fields do (thanks to Attila-Mihaly Balazs).
  • Added a check for mutable default values to PickledObjectField.

Changes in version 1.1.0

  • Added support for Django 2.1 and dropped support for Django < 1.11.

Changes in version 1.0.0

  • Added a new option to prevent a copy of the object before pickling: copy=True
  • Dropped support for Django 1.4
  • Dropped support for Django 1.7
  • Dropped support for Python 3.2
  • Added support for Python 3.6

Changes in version 0.3.2

  • Dropped support for Django 1.3.
  • Dropped support for Python 2.5.
  • Silenced deprecation warnings on Django 1.8+.

Changes in version 0.3.1

  • Favor the built in json module (thanks to Simon Charette).

Changes in version 0.3.0

  • Python 3 support (thanks to Rafal Stozek).

Changes in version 0.2.0

  • Allow pickling of subclasses of django.db.models.Model (thanks to Simon Charette).

Changes in version 0.1.9

  • Added connection and prepared parameters to get_db_prep_value() too (thanks to Matthew Schinckel).

Changes in version 0.1.8

  • Updated link to code repository.

Changes in version 0.1.7

  • Added connection and prepared parameters to get_db_prep_lookup() to get rid of deprecation warnings in Django 1.2.

Changes in version 0.1.6

Changes in version 0.1.5

  • Added support for South.
  • Changed default to null=False, as is common throughout Django.

Changes in version 0.1.4

  • Updated copyright statements.

Changes in version 0.1.3

  • Updated serialization tests (thanks to Michael Fladischer).

Changes in version 0.1.2

  • Added Simplified BSD licence.

Changes in version 0.1.1

  • Added test for serialization.
  • Added note about JSON serialization for browser.
  • Added support for different pickle protocol versions (thanks to Michael Fladischer).

Changes in version 0.1

  • First public release.

Feedback

There is a home page <http://github.com/gintas/django-picklefield> with instructions on how to access the code repository.

Send feedback and suggestions to [email protected] .

Owner
Gintautas Miliauskas
CTO / co-founder at Mavenoid
Gintautas Miliauskas
This is a simple Todo web application built Django (back-end) and React JS (front-end)

Django REST Todo app This is a simple Todo web application built with Django (back-end) and React JS (front-end). The project enables you to systemati

Maxim Mukhin 5 May 06, 2022
Tweak the form field rendering in templates, not in python-level form definitions. CSS classes and HTML attributes can be altered.

django-widget-tweaks Tweak the form field rendering in templates, not in python-level form definitions. Altering CSS classes and HTML attributes is su

Jazzband 1.8k Jan 02, 2023
Simple reproduction of connection leak with celery/django/gevent

Redis connection leak with celery/django/gevent Reproduces celery issue at https://github.com/celery/celery/issues/6819 using gevented django web serv

2 Apr 03, 2022
Vehicle registration using Python, Django and SQlite3

PythonCrud Cadastro de veículos utilizando Python, Django e SQlite3 Para acessar o deploy no Heroku:

Jorge Thiago 4 May 20, 2022
Helps working with singletons - things like global settings that you want to edit from the admin site.

Django Solo +---------------------------+ | | | | | \ | Django Solo helps

Sylvain Toé 726 Jan 08, 2023
Create a netflix-like service using Django, React.js, & More.

Create a netflix-like service using Django. Learn advanced Django techniques to achieve amazing results like never before.

Coding For Entrepreneurs 67 Dec 08, 2022
Service request portal on top of Ansible Tower

Squest - A service request portal based on Ansible Tower Squest is a Web portal that allow to expose Tower based automation as a service. If you want

Hewlett Packard Enterprise 183 Jan 04, 2023
A simple Django middleware for Duo V4 2-factor authentication.

django-duo-universal-auth A lightweight middleware application that adds a layer on top of any number of existing authentication backends, enabling 2F

Adam Angle 1 Jan 10, 2022
A test microblog project created using Django 4.0

django-microblog This is a test microblog project created using Django 4.0. But don't worry this is a fully working project. There is no super-amazing

Ali Kasimoglu 8 Jan 14, 2022
A pickled object field for Django

django-picklefield About django-picklefield provides an implementation of a pickled object field. Such fields can contain any picklable objects. The i

Gintautas Miliauskas 167 Oct 18, 2022
Money fields for Django forms and models.

django-money A little Django app that uses py-moneyed to add support for Money fields in your models and forms. Django versions supported: 1.11, 2.1,

1.4k Jan 06, 2023
A simple Django dev environment setup with docker for demo purposes for GalsenDev community

GalsenDEV Docker Demo This is a basic Django dev environment setup with docker and docker-compose for a GalsenDev Meetup. The main purposes was to mak

3 Jul 03, 2021
No effort, no worry, maximum performance.

Django Cachalot Caches your Django ORM queries and automatically invalidates them. Documentation: http://django-cachalot.readthedocs.io Table of Conte

NoriPyt 980 Jan 06, 2023
A CBV to handle multiple forms in one view

django-shapeshifter A common problem in Django is how to have a view, especially a class-based view that can display and process multiple forms at onc

Kenneth Love 167 Nov 26, 2022
A GitHub Action for checking Django migrations

🔍 Django migrations checker A GitHub Action for checking Django migrations About This repository contains a Github Action that checks Django migratio

Oda 5 Nov 15, 2022
📝 Sticky Notes in Django admin

django-admin-sticky-notes Share notes between superusers. Installation Install via pip: pip install django_admin_sticky_notes Put django_admin_sticky_

Dariusz Choruży 7 Oct 06, 2021
A feature flipper for Django

README Django Waffle is (yet another) feature flipper for Django. You can define the conditions for which a flag should be active, and use it in a num

950 Dec 26, 2022
Send logs to RabbitMQ from Python/Django.

python-logging-rabbitmq Logging handler to ships logs to RabbitMQ. Compatible with Django. Installation Install using pip. pip install python_logging_

Alberto Menendez Romero 38 Nov 17, 2022
Use minify-html, the extremely fast HTML + JS + CSS minifier, with Django.

django-minify-html Use minify-html, the extremely fast HTML + JS + CSS minifier, with Django. Requirements Python 3.8 to 3.10 supported. Django 2.2 to

Adam Johnson 60 Dec 28, 2022
Twitter Bootstrap for Django Form - A simple Django template tag to work with Bootstrap

Twitter Bootstrap for Django Form - A simple Django template tag to work with Bootstrap

tzangms 557 Oct 19, 2022