django-unicorn icon indicating copy to clipboard operation
django-unicorn copied to clipboard

Pagination example?

Open poiedk opened this issue 3 years ago • 8 comments

Is there any example for pagination?

poiedk avatar Aug 24 '21 13:08 poiedk

In this issue there a pagination example: https://github.com/adamghill/django-unicorn/issues/244

nielsonsantana avatar Aug 24 '21 14:08 nielsonsantana

Any recommendations on why I get this error?

serialized_data = orjson.dumps(data, default=_json_serializer)
TypeError: Type is not JSON serializable: Page

I'm using Django 3.2

poiedk avatar Aug 25 '21 15:08 poiedk

@poiedk, you can prevent unserializable(Page,...) and sensitive objects be exposed to javascript by using javascript_exclude to do it.

nielsonsantana avatar Aug 26 '21 19:08 nielsonsantana

Yep, just to follow-up here: I assume you have a Page as a field variable in the Unicorn component view? Can you give me more context about how the Page object is constructed? Is it something custom, or is it generated from Django's Paginator.get_page(...) function? If so, it might make sense for Unicorn to serialize that into something reasonable so others don't run into the same issue.

But for a quick fix, adding the field name to exclude_javascript Meta attribute should help you out.

adamghill avatar Aug 27 '21 00:08 adamghill

I created a working example django-unicorn pagination example

poiedk avatar Sep 08 '21 22:09 poiedk

Another example using django MultipleObjectMixin and adding dynamic filtering and permissions.

https://github.com/trolldbois/django-unicorn-filteredpaginatedview-example

trolldbois avatar Nov 09 '21 23:11 trolldbois

Yep, just to follow-up here: I assume you have a Page as a field variable in the Unicorn component view? Can you give me more context about how the Page object is constructed? Is it something custom, or is it generated from Django's Paginator.get_page(...) function? If so, it might make sense for Unicorn to serialize that into something reasonable so others don't run into the same issue.

But for a quick fix, adding the field name to exclude_javascript Meta attribute should help you out.

@adamghill You can also run into this with the default User object, if you set it to None:

class FindEventsView(UnicornView):
    user: Optional[User] = None

Then in parent view, pass user = None

On change, you'll get:

TypeError: Type is not JSON serializable: User

Don't know why yet. I fixed it by marking it as javascript excluded for now, since I don't need user in the view.

Also happens for anonymous User object.

winrid avatar Oct 27 '23 05:10 winrid

This also applies to pandas dataframes. For example, I have a dataframe being loaded from postgres, which when passed through django-unicorn json serializer, causes auto-typing of columns, e.g. col of str objects gets auto-typed as int64. So adding the following fixed this for me -- in case anyone else is searching for django-unicorn dataframe handling errors (which chatgpt could not intuit). class Meta: javascript_exclude = ("dataframe_name", )

I feel like dataframe handling and reactive django-unicorn go well together when it actually works so maybe a feature request to build this out more would be appropriate. Debugging this auto-typing thing took longer than one would expect.

vbartle avatar Nov 23 '23 15:11 vbartle