-
-
Notifications
You must be signed in to change notification settings - Fork 119
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
Comments
Hello, The issue you're encountering is because Django Unicorn needs to serialize (pickle) the component state, but the get_elided_page_range() method returns a generator object which can't be pickled. Here's a solution that addresses this by creating serializable versions of the pagination objects: from django.core.paginator import Page, Paginator
from django_unicorn.components import UnicornView
class SerializablePaginator:
"""A JSON-serializable version of Django's Paginator"""
def __init__(self, paginator):
self.num_pages = paginator.num_pages
self.count = paginator.count
self.ELLIPSIS = paginator.ELLIPSIS
self._elided_page_range = None
def get_elided_page_range(self, **kwargs):
"""Return the elided page range if it was set"""
return self._elided_page_range if self._elided_page_range is not None else []
class SerializablePage:
"""A JSON-serializable version of Django's Page object"""
def __init__(self, page_obj):
self.object_list = list(page_obj.object_list)
self.number = page_obj.number
self.has_next = page_obj.has_next()
self.has_previous = page_obj.has_previous()
self.next_page_number = page_obj.next_page_number() if page_obj.has_next() else None
self.previous_page_number = page_obj.previous_page_number() if page_obj.has_previous() else None
# Store paginator and convert elided page range to list
self.paginator = SerializablePaginator(page_obj.paginator)
self.paginator._elided_page_range = list(page_obj.paginator.get_elided_page_range(
number=page_obj.number,
on_each_side=1,
on_ends=1
))
# Example usage in your view
class MultimediaListView(UnicornView):
items_per_page = 6
page_index = 1
def paginate(self):
paginator = Paginator(self.multimedia, self.items_per_page)
page_obj = paginator.page(self.page_index)
# Convert to serializable format
self.page = SerializablePage(page_obj)
self.items = self.page.object_list The key changes are:
This solution maintains all the pagination functionality while making it compatible with Django Unicorn's component state serialization. |
I have this list view that requires pagination and i have a generator for paginating but i keep getting this warning:
This is the code:
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?
The text was updated successfully, but these errors were encountered: