agateblue / django-dynamic-preferences

Dynamic global and instance settings for your django project
https://django-dynamic-preferences.readthedocs.org/
BSD 3-Clause "New" or "Revised" License
348 stars 86 forks source link

Added ability to pickle FilePreference in Django 4.2 #297

Closed mihalikv closed 1 year ago

mihalikv commented 1 year ago

If you have Django 4.2 and latest django-dynamic-preferences==1.15.0 and you try to do something like this:

import pickle
from dynamic_preferences.types import  FilePreference
from dynamic_preferences.registries import global_preferences_registry

global_preferences = global_preferences_registry.manager()

@register_preference
class AllCategoryImage(FilePreference):
    name = 'all_category_image'
    verbose_name = 'Header image for catalog'

item = global_preferences['catalog__all_category_image']
pickled_result = pickle.dumps(item)

It will result in the error AttributeError: Can't pickle local object 'PreferenceFieldFile.__init__.<locals>.FakeInstance'

It worked well with Django 2.x but in newer Django also field and instance attributes are added to __setstate__ -> https://github.com/django/django/commit/f600e3fad6e92d9fe1ad8b351dc8446415f24345