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

Issue using orm "values" on share middleware #43

Open
mauriciodotso opened this issue Apr 13, 2024 · 0 comments
Open

Issue using orm "values" on share middleware #43

mauriciodotso opened this issue Apr 13, 2024 · 0 comments

Comments

@mauriciodotso
Copy link

Hi, so I had issues when trying to limit the data returned in the share method called inside a middleware. I was doing something like the following:

    def middleware(request):
        if request.user:
            share(
                request,
                tenants=lambda: request.user.profiles.select_related(
                    "organization"
                ).values("organization_id", "organization__name"),
            )

        return get_response(request)

The issue happens inside the InertiaJsonEncoder on here. As it was trying to pass a dictionary to the method model_to_dict.

The solution I found for this was to add a custom Json encoder as explained in the documentation and do the following:

    def default(self, value):
        if isinstance(value, QuerySet):
            return [self.default(model) for model in value]

        if isinstance(value, dict):
            return value

        return super().default(value)
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