Open poiedk opened 3 years ago
In this issue there a pagination example: https://github.com/adamghill/django-unicorn/issues/244
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, you can prevent unserializable(Page
,...) and sensitive objects be exposed to javascript by using javascript_exclude
to do it.
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.
I created a working example django-unicorn pagination example
Another example using django MultipleObjectMixin and adding dynamic filtering and permissions.
https://github.com/trolldbois/django-unicorn-filteredpaginatedview-example
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 thePage
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.
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.
Is there any example for pagination?