I’m trying to serialize and object in my view. This is an object i’m building for the context of my page and it is a python dictionnary, including django models and forms.
As i’m trying to cache this object with the low-level API of django i first tried the classic way :
cache.set("my_dict", my_dict, 60*10)
but i had this error :
Can’t pickle <function …”
The default serializer of django can’t serialize this dictionnary.
So i tried to serialize it with json with these lines :
my_dict is created with several django requests, also SQL requests in an other database but at the end the data in my_dict are in string or int types, and some functions to generate python lists or dictionnaries.
So if you need all of the types included in my_dict here is an exemple :
my_dict = { "key_1" : <MyModel: MY_OBJECT (96)>, #Model object
"key_2": ['1','2','3','4'], #Python list
"key_3" : {'id_1':[<MyModel: MY_OBJECT (96)> , <MyForm bound=False, valid=Unknown, fields=(field_1;field_2)> ],...}, #python lists of django objects and forms
"key_4": datetime.datetime(2020, 5, 17) #Datetime object
It is a fictitional dictionnary but you have a tour of all types of objects included in it.
And to give you the full error i had when using pickle serializer :
Can’t pickle <function paginator_number …>: it’s not the same object as django.contrib.admin.templatetags.admin_list.paginator_number
What’s strange is that i don"t use pagination in this view, nor in the model’s methods or forms.
You’re trying to address a very specific error. Posting a “representative example” of what you think my_dict contains when this error occurs is not sufficient for answering your question about how a paginator is involved here.
For us to be able to have any realistic chance to diagnose this, we’re going to need to see the complete view where my_dict is being created, and that’s just as a starting point. This may extend into the forms and model definitions as well.
I understand yes.
The situation has changed a little, I now know which parts of the dictionary generate this error and it is the django forms.
I also decided to use the session method of django instead of cache directly, even if it still use cache system, but the error is the same cause it also needs to serialize my object.
I generate pre-filled forms that I integrate into my_dict, and they create the serialization error.
and here the view i tested : (i know my_dict i not a dictionnary in this exemple but it is to keep consistency with my previous posts)
def Main(request, abo, cmv):
#session cache update
my_dict= request.session.get("my_dict", None)
if not my_dict:
my_dict = PatientForm(initial={'patient_abo':abo,'patient_cmv':cmv}) #prefilled form
request.session['my_dict'] = my_dict #ERROR HERE when trying to add in session cache
#if form submit in template
if request.method == 'POST' and 'submitForm' in request.POST:
my_form = PatientForm(request.POST)
is my_form.is_valid():
patient_object = my_form.save(commit=False)
patient_object.save()
#update session
request.session['my_dict']= PatientForm(initial={'patient_abo':patient_object.patient_abo,'patient_cmv':patient_object.patient_cmv})
return render(request, 'Register/main.html', {'my_dict':my_dict})
and the error is still the same :
_pickle.PicklingError: Can’t pickle <function paginator_number at 0x0000015D4200C160>: it’s not the same object as django.contrib.admin.templatetags.admin_list.paginator_number
abo is a string from a list of choices. And cmv a free string.
I am sure of the good compatibility of these variables with the form. I tested it several times.
It’s not a question of it being compatible with the forms. Forms are designed to work with as many datatypes as makes sense.
It’s a question of whether or not those objects are directly serializable.
Edit: Playing around a little bit in the shell is starting to give me the impression that Form objects are not serializable - at least not by the built-in serializer. It simply may be that you need to extract the data from the fields and serialize that independently of the Form object itself - or provide a custom serializer.
i understand. My big question is about the error on a “function”, because I did not define a function in my form. And even less about pagination so i assume it may come from something else.
I didn’t find anything in forums about this specific error. Maybe have you seen other types of pickle’ error about function ? If so, what was the source ?
I would suggest that you’d be better off just serializing the initial attribute of the form and using that to reconstruct the form when you need it.
Forms are constructed with a fair amount of metaclass code, similar to how Models are built. They aren’t “trivial” objects. (see the source code in django.forms.forms)