Skip to content

Issue using orm "values" on share middleware #43

Open
@mauriciodotso

Description

@mauriciodotso

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions