django-picklefield-3.0.1/0000775000372000037200000000000013666466745016117 5ustar travistravis00000000000000django-picklefield-3.0.1/django_picklefield.egg-info/0000775000372000037200000000000013666466745023406 5ustar travistravis00000000000000django-picklefield-3.0.1/django_picklefield.egg-info/requires.txt0000664000372000037200000000003113666466745026000 0ustar travistravis00000000000000Django>=2.2 [tests] tox django-picklefield-3.0.1/django_picklefield.egg-info/top_level.txt0000664000372000037200000000001413666466745026133 0ustar travistravis00000000000000picklefield django-picklefield-3.0.1/django_picklefield.egg-info/dependency_links.txt0000664000372000037200000000000113666466745027454 0ustar travistravis00000000000000 django-picklefield-3.0.1/django_picklefield.egg-info/SOURCES.txt0000664000372000037200000000051113666466745025267 0ustar travistravis00000000000000LICENSE MANIFEST.in README.rst setup.cfg setup.py django_picklefield.egg-info/PKG-INFO django_picklefield.egg-info/SOURCES.txt django_picklefield.egg-info/dependency_links.txt django_picklefield.egg-info/requires.txt django_picklefield.egg-info/top_level.txt picklefield/__init__.py picklefield/constants.py picklefield/fields.pydjango-picklefield-3.0.1/django_picklefield.egg-info/PKG-INFO0000664000372000037200000003227013666466745024507 0ustar travistravis00000000000000Metadata-Version: 2.1 Name: django-picklefield Version: 3.0.1 Summary: Pickled object field for Django Home-page: http://github.com/gintas/django-picklefield Author: Simon Charette Author-email: charette.s+django-picklefiel@gmail.com License: MIT Description: django-picklefield ================== .. image:: https://img.shields.io/pypi/l/django-picklefield.svg?style=flat :target: https://pypi.python.org/pypi/django-picklefield/ :alt: License .. image:: https://img.shields.io/pypi/v/django-picklefield.svg?style=flat :target: https://pypi.python.org/pypi/django-picklefield/ :alt: Latest Version .. image:: https://travis-ci.org/gintas/django-picklefield.svg?branch=master :target: https://travis-ci.org/gintas/django-picklefield :alt: Build Status .. image:: https://coveralls.io/repos/gintas/django-picklefield/badge.svg?branch=master :target: https://coveralls.io/r/gintas/django-picklefield?branch=master :alt: Coverage Status .. image:: https://img.shields.io/pypi/pyversions/django-picklefield.svg?style=flat :target: https://pypi.python.org/pypi/django-picklefield/ :alt: Supported Python Versions .. image:: https://img.shields.io/pypi/wheel/django-picklefield.svg?style=flat :target: https://pypi.python.org/pypi/django-picklefield/ :alt: 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. .. _Django snippet #1694: http://www.djangosnippets.org/snippets/1694/ .. _Django snippet #513: http://www.djangosnippets.org/snippets/513/ ----- 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: .. code:: python >>> 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: .. code:: python >>> 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 .. _Tox: https://testrun.org/tox/latest/ -------------- 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 ======================== * Fixed South support (thanks aehlke@github). 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 with instructions on how to access the code repository. Send feedback and suggestions to gintautas@miliauskas.lt . Keywords: django pickle model field Platform: UNKNOWN Classifier: Development Status :: 5 - Production/Stable Classifier: Environment :: Web Environment Classifier: Framework :: Django Classifier: Framework :: Django :: 2.2 Classifier: Framework :: Django :: 3.0 Classifier: Intended Audience :: Developers Classifier: License :: OSI Approved :: MIT License Classifier: Operating System :: OS Independent Classifier: Programming Language :: Python Classifier: Programming Language :: Python :: 3 :: Only Classifier: Programming Language :: Python :: 3 Classifier: Programming Language :: Python :: 3.5 Classifier: Programming Language :: Python :: 3.6 Classifier: Programming Language :: Python :: 3.7 Classifier: Programming Language :: Python :: 3.8 Classifier: Topic :: Software Development :: Libraries :: Application Frameworks Classifier: Topic :: Software Development :: Libraries :: Python Modules Requires-Python: >=3 Provides-Extra: tests django-picklefield-3.0.1/README.rst0000664000372000037200000002331613666466676017616 0ustar travistravis00000000000000django-picklefield ================== .. image:: https://img.shields.io/pypi/l/django-picklefield.svg?style=flat :target: https://pypi.python.org/pypi/django-picklefield/ :alt: License .. image:: https://img.shields.io/pypi/v/django-picklefield.svg?style=flat :target: https://pypi.python.org/pypi/django-picklefield/ :alt: Latest Version .. image:: https://travis-ci.org/gintas/django-picklefield.svg?branch=master :target: https://travis-ci.org/gintas/django-picklefield :alt: Build Status .. image:: https://coveralls.io/repos/gintas/django-picklefield/badge.svg?branch=master :target: https://coveralls.io/r/gintas/django-picklefield?branch=master :alt: Coverage Status .. image:: https://img.shields.io/pypi/pyversions/django-picklefield.svg?style=flat :target: https://pypi.python.org/pypi/django-picklefield/ :alt: Supported Python Versions .. image:: https://img.shields.io/pypi/wheel/django-picklefield.svg?style=flat :target: https://pypi.python.org/pypi/django-picklefield/ :alt: 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. .. _Django snippet #1694: http://www.djangosnippets.org/snippets/1694/ .. _Django snippet #513: http://www.djangosnippets.org/snippets/513/ ----- 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: .. code:: python >>> 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: .. code:: python >>> 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 .. _Tox: https://testrun.org/tox/latest/ -------------- 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 ======================== * Fixed South support (thanks aehlke@github). 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 with instructions on how to access the code repository. Send feedback and suggestions to gintautas@miliauskas.lt . django-picklefield-3.0.1/MANIFEST.in0000664000372000037200000000004213666466676017654 0ustar travistravis00000000000000include LICENSE include README.rstdjango-picklefield-3.0.1/setup.py0000664000372000037200000000306313666466676017636 0ustar travistravis00000000000000from __future__ import unicode_literals from setuptools import find_packages, setup import picklefield with open('README.rst') as file_: long_description = file_.read() setup( name='django-picklefield', version=picklefield.__version__, description='Pickled object field for Django', long_description=long_description, author='Simon Charette', author_email='charette.s+django-picklefiel@gmail.com', url='http://github.com/gintas/django-picklefield', license='MIT', classifiers=[ 'Development Status :: 5 - Production/Stable', 'Environment :: Web Environment', 'Framework :: Django', 'Framework :: Django :: 2.2', 'Framework :: Django :: 3.0', 'Intended Audience :: Developers', 'License :: OSI Approved :: MIT License', 'Operating System :: OS Independent', 'Programming Language :: Python', 'Programming Language :: Python :: 3 :: Only', 'Programming Language :: Python :: 3', 'Programming Language :: Python :: 3.5', 'Programming Language :: Python :: 3.6', 'Programming Language :: Python :: 3.7', 'Programming Language :: Python :: 3.8', 'Topic :: Software Development :: Libraries :: Application Frameworks', 'Topic :: Software Development :: Libraries :: Python Modules', ], keywords=['django pickle model field'], packages=find_packages(exclude=['tests', 'tests.*']), python_requires='>=3', install_requires=['Django>=2.2'], extras_require={ 'tests': ['tox'], }, ) django-picklefield-3.0.1/LICENSE0000664000372000037200000000214613666466676017132 0ustar travistravis00000000000000Copyright (c) 2009-2010 Gintautas Miliauskas Copyright (c) 2011-2018 Simon Charette Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. django-picklefield-3.0.1/setup.cfg0000664000372000037200000000026413666466745017742 0ustar travistravis00000000000000[flake8] max-line-length = 119 [isort] combine_as_imports = true include_trailing_comma = true multi_line_output = 5 not_skip = __init__.py [egg_info] tag_build = tag_date = 0 django-picklefield-3.0.1/PKG-INFO0000664000372000037200000003227013666466745017220 0ustar travistravis00000000000000Metadata-Version: 2.1 Name: django-picklefield Version: 3.0.1 Summary: Pickled object field for Django Home-page: http://github.com/gintas/django-picklefield Author: Simon Charette Author-email: charette.s+django-picklefiel@gmail.com License: MIT Description: django-picklefield ================== .. image:: https://img.shields.io/pypi/l/django-picklefield.svg?style=flat :target: https://pypi.python.org/pypi/django-picklefield/ :alt: License .. image:: https://img.shields.io/pypi/v/django-picklefield.svg?style=flat :target: https://pypi.python.org/pypi/django-picklefield/ :alt: Latest Version .. image:: https://travis-ci.org/gintas/django-picklefield.svg?branch=master :target: https://travis-ci.org/gintas/django-picklefield :alt: Build Status .. image:: https://coveralls.io/repos/gintas/django-picklefield/badge.svg?branch=master :target: https://coveralls.io/r/gintas/django-picklefield?branch=master :alt: Coverage Status .. image:: https://img.shields.io/pypi/pyversions/django-picklefield.svg?style=flat :target: https://pypi.python.org/pypi/django-picklefield/ :alt: Supported Python Versions .. image:: https://img.shields.io/pypi/wheel/django-picklefield.svg?style=flat :target: https://pypi.python.org/pypi/django-picklefield/ :alt: 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. .. _Django snippet #1694: http://www.djangosnippets.org/snippets/1694/ .. _Django snippet #513: http://www.djangosnippets.org/snippets/513/ ----- 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: .. code:: python >>> 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: .. code:: python >>> 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 .. _Tox: https://testrun.org/tox/latest/ -------------- 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 ======================== * Fixed South support (thanks aehlke@github). 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 with instructions on how to access the code repository. Send feedback and suggestions to gintautas@miliauskas.lt . Keywords: django pickle model field Platform: UNKNOWN Classifier: Development Status :: 5 - Production/Stable Classifier: Environment :: Web Environment Classifier: Framework :: Django Classifier: Framework :: Django :: 2.2 Classifier: Framework :: Django :: 3.0 Classifier: Intended Audience :: Developers Classifier: License :: OSI Approved :: MIT License Classifier: Operating System :: OS Independent Classifier: Programming Language :: Python Classifier: Programming Language :: Python :: 3 :: Only Classifier: Programming Language :: Python :: 3 Classifier: Programming Language :: Python :: 3.5 Classifier: Programming Language :: Python :: 3.6 Classifier: Programming Language :: Python :: 3.7 Classifier: Programming Language :: Python :: 3.8 Classifier: Topic :: Software Development :: Libraries :: Application Frameworks Classifier: Topic :: Software Development :: Libraries :: Python Modules Requires-Python: >=3 Provides-Extra: tests django-picklefield-3.0.1/picklefield/0000775000372000037200000000000013666466745020372 5ustar travistravis00000000000000django-picklefield-3.0.1/picklefield/fields.py0000664000372000037200000002011013666466676022207 0ustar travistravis00000000000000from base64 import b64decode, b64encode from copy import deepcopy from pickle import dumps, loads from zlib import compress, decompress from django.conf import settings from django.core import checks from django.db import models from django.utils.encoding import force_str from .constants import DEFAULT_PROTOCOL class PickledObject(str): """ A subclass of string so it can be told whether a string is a pickled object or not (if the object is an instance of this class then it must [well, should] be a pickled one). Only really useful for passing pre-encoded values to ``default`` with ``dbsafe_encode``, not that doing so is necessary. If you remove PickledObject and its references, you won't be able to pass in pre-encoded values anymore, but you can always just pass in the python objects themselves. """ class _ObjectWrapper: """ A class used to wrap object that have properties that may clash with the ORM internals. For example, objects with the `prepare_database_save` property such as `django.db.Model` subclasses won't work under certain conditions and the same apply for trying to retrieve any `callable` object. """ __slots__ = ('_obj',) def __init__(self, obj): self._obj = obj def wrap_conflictual_object(obj): if hasattr(obj, 'prepare_database_save') or callable(obj): obj = _ObjectWrapper(obj) return obj def get_default_protocol(): return getattr(settings, 'PICKLEFIELD_DEFAULT_PROTOCOL', DEFAULT_PROTOCOL) def dbsafe_encode(value, compress_object=False, pickle_protocol=None, copy=True): # We use deepcopy() here to avoid a problem with cPickle, where dumps # can generate different character streams for same lookup value if # they are referenced differently. # The reason this is important is because we do all of our lookups as # simple string matches, thus the character streams must be the same # for the lookups to work properly. See tests.py for more information. if pickle_protocol is None: pickle_protocol = get_default_protocol() if copy: # Copy can be very expensive if users aren't going to perform lookups # on the value anyway. value = deepcopy(value) value = dumps(value, protocol=pickle_protocol) if compress_object: value = compress(value) value = b64encode(value).decode() # decode bytes to str return PickledObject(value) def dbsafe_decode(value, compress_object=False): value = value.encode() # encode str to bytes value = b64decode(value) if compress_object: value = decompress(value) return loads(value) class PickledObjectField(models.Field): """ A field that will accept *any* python object and store it in the database. PickledObjectField will optionally compress its values if declared with the keyword argument ``compress=True``. Does not actually encode and compress ``None`` objects (although you can still do lookups using None). This way, it is still possible to use the ``isnull`` lookup type correctly. """ empty_strings_allowed = False def __init__(self, *args, **kwargs): self.compress = kwargs.pop('compress', False) protocol = kwargs.pop('protocol', None) if protocol is None: protocol = get_default_protocol() self.protocol = protocol self.copy = kwargs.pop('copy', True) kwargs.setdefault('editable', False) super().__init__(*args, **kwargs) def get_default(self): """ Returns the default value for this field. The default implementation on models.Field calls force_unicode on the default, which means you can't set arbitrary Python objects as the default. To fix this, we just return the value without calling force_unicode on it. Note that if you set a callable as a default, the field will still call it. It will *not* try to pickle and encode it. """ if self.has_default(): if callable(self.default): return self.default() return self.default # If the field doesn't have a default, then we punt to models.Field. return super().get_default() def _check_default(self): if self.has_default() and isinstance(self.default, (list, dict, set)): return [ checks.Warning( "%s default should be a callable instead of a mutable instance so " "that it's not shared between all field instances." % ( self.__class__.__name__, ), hint=( 'Use a callable instead, e.g., use `%s` instead of ' '`%r`.' % ( type(self.default).__name__, self.default, ) ), obj=self, id='picklefield.E001', ) ] else: return [] def check(self, **kwargs): errors = super().check(**kwargs) errors.extend(self._check_default()) return errors def deconstruct(self): name, path, args, kwargs = super().deconstruct() if self.compress: kwargs['compress'] = True if self.protocol != get_default_protocol(): kwargs['protocol'] = self.protocol return name, path, args, kwargs def to_python(self, value): """ B64decode and unpickle the object, optionally decompressing it. If an error is raised in de-pickling and we're sure the value is a definite pickle, the error is allowed to propagate. If we aren't sure if the value is a pickle or not, then we catch the error and return the original value instead. """ if value is not None: try: value = dbsafe_decode(value, self.compress) except Exception: # If the value is a definite pickle; and an error is raised in # de-pickling it should be allowed to propagate. if isinstance(value, PickledObject): raise else: if isinstance(value, _ObjectWrapper): return value._obj return value def pre_save(self, model_instance, add): value = super().pre_save(model_instance, add) return wrap_conflictual_object(value) def from_db_value(self, value, expression, connection): return self.to_python(value) def get_db_prep_value(self, value, connection=None, prepared=False): """ Pickle and b64encode the object, optionally compressing it. The pickling protocol is specified explicitly (by default 2), rather than as -1 or HIGHEST_PROTOCOL, because we don't want the protocol to change over time. If it did, ``exact`` and ``in`` lookups would likely fail, since pickle would now be generating a different string. """ if value is not None and not isinstance(value, PickledObject): # We call force_str here explicitly, so that the encoded string # isn't rejected by the postgresql_psycopg2 backend. Alternatively, # we could have just registered PickledObject with the psycopg # marshaller (telling it to store it like it would a string), but # since both of these methods result in the same value being stored, # doing things this way is much easier. value = force_str(dbsafe_encode(value, self.compress, self.protocol, self.copy)) return value def value_to_string(self, obj): value = self.value_from_object(obj) return self.get_db_prep_value(value) def get_internal_type(self): return 'TextField' def get_lookup(self, lookup_name): """ We need to limit the lookup types. """ if lookup_name not in ['exact', 'in', 'isnull']: raise TypeError('Lookup type %s is not supported.' % lookup_name) return super().get_lookup(lookup_name) django-picklefield-3.0.1/picklefield/__init__.py0000664000372000037200000000047513666466676022514 0ustar travistravis00000000000000from __future__ import unicode_literals import django.utils.version from .constants import DEFAULT_PROTOCOL from .fields import PickledObjectField __all__ = 'VERSION', '__version__', 'DEFAULT_PROTOCOL', 'PickledObjectField' VERSION = (3, 0, 1, 'final', 0) __version__ = django.utils.version.get_version(VERSION) django-picklefield-3.0.1/picklefield/constants.py0000664000372000037200000000002513666466676022760 0ustar travistravis00000000000000DEFAULT_PROTOCOL = 2