adamghill / django-unicorn

The magical reactive component framework for Django ✨
https://www.django-unicorn.com
MIT License
2.37k stars 119 forks source link

Pagination example? #263

Open poiedk opened 3 years ago

poiedk commented 3 years ago

Is there any example for pagination?

nielsonsantana commented 3 years ago

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

poiedk commented 3 years ago

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

nielsonsantana commented 3 years ago

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

adamghill commented 3 years ago

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.

poiedk commented 3 years ago

I created a working example django-unicorn pagination example

trolldbois commented 2 years ago

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

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

winrid commented 1 year ago

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.

vbartle commented 11 months ago

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.