Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot cache component - cannot pickle 'generator' object #594

Open
hamidrabedi opened this issue Sep 6, 2023 · 0 comments
Open

Cannot cache component - cannot pickle 'generator' object #594

hamidrabedi opened this issue Sep 6, 2023 · 0 comments

Comments

@hamidrabedi
Copy link

I have this list view that requires pagination and i have a generator for paginating but i keep getting this warning:

Cannot cache component '<class 'pages.components.multimedia_list.MultimediaListView'>' because it is not picklable: <class 'TypeError'>: cannot pickle 'generator' object

This is the code:

from django.db.models import QuerySet
from django.core.paginator import Paginator

from django_unicorn.components import UnicornView

from pages.models import MultiMedia


class MultimediaListView(UnicornView):
    multimedia: QuerySet[MultiMedia] = None
    items = None
    items_per_page = 6  # Number of items to display per page
    page_index = 1  # Current page index
    paginator = None  # Paginator object
    page = None  # Current page
    page_range = None  # Range of pages to display

    class Meta:
        """
        Meta class for additional settings.
        """
        javascript_exclude = (
            "paginator",
            "page",
            "page_range",
            "multimedia",
        )  # Fields to exclude from JavaScript

    def mount(self):
        """
        Prepares the view for rendering.
        """
        self.paginate()

    def paginate(self):
        """
        Paginates the multimedia based on the current page index and items per page.
        """
        paginator = Paginator(self.multimedia, self.items_per_page)
        self.paginator = paginator
        self.page = self.paginator.page(self.page_index)
        self.page_range = self.paginator.get_elided_page_range(number=self.page_index, on_each_side=3, on_ends=2)
        self.items = self.page.object_list

    def go_to_page(self, page):
        """
        Changes the current page to the specified page and paginates the multimedia.
        """
        self.page_index = page
        self.page = ''
        self.paginate()

Which page_range is the generator object, is there anyway to fix this?
and why pickling and caching all the attributes is necessary? like why should it be done?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant