Pickle field implementation for Django.

Modified for Evennia by Griatch and the Evennia community.

class evennia.utils.picklefield.PickledObject[source]

Bases: 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.

evennia.utils.picklefield.dbsafe_encode(value, compress_object=False, pickle_protocol=4)[source]
evennia.utils.picklefield.dbsafe_decode(value, compress_object=False)[source]
class evennia.utils.picklefield.PickledWidget(attrs=None)[source]

Bases: django.forms.widgets.Textarea

This is responsible for outputting HTML representing a given field.

render(name, value, attrs=None, renderer=None)[source]

Display of the PickledField in django admin

value_from_datadict(data, files, name)[source]

Given a dictionary of data and this widget’s name, return the value of this widget or None if it’s not provided.

property media
class evennia.utils.picklefield.PickledFormField(*args, **kwargs)[source]

Bases: django.forms.fields.CharField

This represents one input field for the form.


alias of PickledWidget

default_error_messages = {'invalid': 'This is not a Python Literal. You can store things like strings, integers, or floats, but you must do it by typing them as you would type them in the Python Interpreter. For instance, strings must be surrounded by quote marks. We have converted it to a string for your convenience. If it is acceptable, please hit save again.', 'required': 'This field is required.'}
__init__(*args, **kwargs)[source]

Initialize self. See help(type(self)) for accurate signature.


Validate the given value and return its “cleaned” value as an appropriate Python object. Raise ValidationError for any errors.

class evennia.utils.picklefield.PickledObjectField(*args, **kwargs)[source]

Bases: django.db.models.fields.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.

__init__(*args, **kwargs)[source]

Initialize self. See help(type(self)) for accurate signature.


Returns the default value for this field.

The default implementation on models.Field calls force_str 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_str 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.

from_db_value(value, *args)[source]

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.


Return a django.forms.Field instance for this field.

pre_save(model_instance, add)[source]

Return field’s value just before saving.

get_db_prep_value(value, connection=None, prepared=False)[source]

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.


Return a string value of this field from the passed obj. This is used by the serialization framework.

get_db_prep_lookup(lookup_type, value, connection=None, prepared=False)[source]