Skip to content

🐛 Incoherent cache results #304

Closed

Description

@debnet

Bug Description

In a multi-user application, I have a select2 field (overriding widget queryset according to the user, see code below) which retrieve data according to the workspace of the current authenticated user, but a user can switch workspace at will through a menu and "sometimes" the select2 list displays results from the previous workspace, and "rarely" it even displays results from other users' workspaces, which is problematic. It looks like a cache issue to me.

In my modest opinion, a solution could be to have the ability to add a cache prefix key per user or configurable directly on the widget and/or the form.

class LinkForm(ModelForm):
    tags = forms.ModelMultipleChoiceField(
        required=False,
        label=_("Tags"),
        queryset=Tag.objects.order_by("order", "name"),
        widget=ModelSelect2MultipleWidget(
            queryset=Tag.objects.order_by("order", "name"),
            search_fields=("code__icontains", "name__icontains"),
            attrs={"data-minimum-input-length": 0},
        ),
    )

    def __init__(self, *args, request=None, **kwargs):
        self.request = request
        super().__init__(*args, **kwargs)
        if self.request:
            field = self.fields["tags"]
            workspace = self.request.session.get("current_workspace")
            if workspace:
                field.queryset = field.queryset.filter(workspace_id=workspace)
            else:
                field.queryset = field.queryset.none()
            field.widget.queryset = field.queryset

    class Meta:
        model = Link
        fields = (
            "tags",
        )

Steps to Reproduce

  1. User A, using Workspace 1, see list items from Workspace 1.
  2. User A, switch to Workspace 2, see list items from Workspace 2.
  3. User A, switch to Workspace 1 again, see list items from Workspace 2 (?).

?) User B, using Workspace 9, see list items from Workpace 1 which belongs to User A.

Expected Behavior

I expect the results from select2 droplists to be coherent with the queryset.

Activity

  1. Repository owner locked and limited conversation to collaborators on Dec 10, 2024
  2. converted this issue into a discussion #306 on Dec 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions