Low-level cache.set raises the error : Can't pickle <function paginator_number ..>: it's not the same object as django.contrib.admin.templatetags.admin_list.paginator_number


I’m trying to use the low-level cache method of Django to keep in memory two dictionnaries i use in my context’s view and that i sometimes need to update, sometimes not.

Here is my settings (but it seems to work well):

    'default': {
        'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache',
        'LOCATION': '/var/tmp/django_cache',

And here is my view

def Records(request):
    #Get cache values or update if not existing (first load or expired)
    dic_1 = cache.get_or_set("dic_1", generate_dic_1() , 10*60)
    dic_2 = cache.get_or_set("dic_2", generate_dic_2() , 10*60)

    #update variablesif buttons clicked
    if request.method == 'POST' and "change_dic_1_button" in request.POST:
        dic_1 = generate_dic_1()
        cache.set("dic_1", dic_1 , 10*60)

    elif request.method == 'POST' and "change_dic_2_button" in request.POST:
        dic_2 = generate_dic_2()
        cache.set("dic_2", dic_1 , 10*60)

    return render(request,"Records\myPage.html", {"dic_1" : dic_1; "dic_2" : dic_2})

So in my template i have two buttons, if i click one i want to update dic_1 but updating dic_2 is not necessary, and vis versa.

The problem is that i got an error when i click on a button when the code try to cache.set the corresponding dictionnary. But what i don’t understand is that my error is like

“Can’t pickle <function paginator_number …>: it’s not the same object as django.contrib.admin.templatetags.admin_list.paginator_number”

What i don’t undestand is that i don’t use paginator in this template, and even less in my dictionaries…

I’m sure that the first cache.set before the request check is working well, but the second when it enter the if is raising this error.

Do I use the cache correctly ? Where can it come from ?

Thank you

What are generate_dict_1 and generate_dict_2?

Thank you for your answer,
These are fake functions so as not to clutter the post with code that works and is not directly related to cache management. What I mean is that in these functions I simply modify dic_1 or dic_2 but it would take a long time to write everything and in my opinion irrelevant.
What I want to know is especially if my use of the low-level cache seems correct to you.

I’m not familiar with the use of the cache and i think i can “save” some variables of my view’s context so i don’t need to regenerate them for each button of ma page i click on.

What i can say also is that i don’t use pagination in my view or my template. I use it for an other page but not this one.

I hope i gave enough details. Let me know if it’s not the case.

Thank you

What is relevant is what the data type of return value from those functions is, and what is contained within that datatype.

I have several types of objects in these python dictionnaries :

  • Django Models,
  • Django Forms,
  • Python lists
  • python dictionnaries
  • char, text, int, float, dates, bool

And what is strange is that when i raise this error, it stays and i can’t reload any page before i rerun the server.

I undestand that i maybe don’t give you enough data, but if you can’t answer about the error, maybe you can answer about my method of recording the two variables in the cache and then calling cache.set to update some saved variables ?

That’s something you can experiment with yourself if you’re not comfortable with the API. It works really well in the Django shell.