From e583e78a4e7827426b1e780d7f6d234c7953a7ed Mon Sep 17 00:00:00 2001 From: Sli Date: Mon, 21 Oct 2024 17:11:07 +0200 Subject: [PATCH] Convert the whole request to json at once on select widget --- .../core/components/ajax-select-base.ts | 5 ++-- .../core/widgets/autocomplete_select.jinja | 2 +- core/views/widgets/select.py | 29 ++++++++++--------- 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/core/static/webpack/core/components/ajax-select-base.ts b/core/static/webpack/core/components/ajax-select-base.ts index de3fdfa8..0befd398 100644 --- a/core/static/webpack/core/components/ajax-select-base.ts +++ b/core/static/webpack/core/components/ajax-select-base.ts @@ -165,9 +165,8 @@ export abstract class AjaxSelect extends AutoCompleteSelectBase { connectedCallback() { /* Capture initial values before they get moved to the inner node and overridden by tom-select */ - this.initialValues = Array.from(this.children) - .filter((child: Element) => child.tagName.toLowerCase() === "slot") - .map((slot) => JSON.parse(slot.innerHTML)); + const initial = this.querySelector("slot[name='initial']")?.textContent; + this.initialValues = initial ? JSON.parse(initial) : []; super.connectedCallback(); } diff --git a/core/templates/core/widgets/autocomplete_select.jinja b/core/templates/core/widgets/autocomplete_select.jinja index a051c202..1858cccd 100644 --- a/core/templates/core/widgets/autocomplete_select.jinja +++ b/core/templates/core/widgets/autocomplete_select.jinja @@ -9,5 +9,5 @@ {% endif %}{% for widget in group_choices %} {% include widget.template_name %}{% endfor %}{% if group_name %} {% endif %}{% endfor %} -{% for sel in selected %}{{ sel }}{% endfor %} +{% if initial %}{{ initial }}{% endif %} \ No newline at end of file diff --git a/core/views/widgets/select.py b/core/views/widgets/select.py index bd910059..bc5b2711 100644 --- a/core/views/widgets/select.py +++ b/core/views/widgets/select.py @@ -2,6 +2,7 @@ from django.contrib.staticfiles.storage import staticfiles_storage from django.db.models import Model from django.forms import Select, SelectMultiple from ninja import ModelSchema +from pydantic import TypeAdapter from core.models import Group, SithFile, User from core.schemas import GroupSchema, SithFileSchema, UserProfileSchema @@ -46,20 +47,22 @@ class AutoCompleteSelectMixin: "css": [staticfiles_storage.url(file) for file in self.css], } if self.is_ajax: - context["selected"] = [ - self.schema.from_orm(obj).model_dump_json() - for obj in self.model.objects.filter( - **{ - f"{self.pk}__in": [ - pk - for pk in context["widget"]["value"] - if str( + adapter = TypeAdapter(list[self.schema]) + context["initial"] = adapter.dump_json( + adapter.validate_python( + self.model.objects.filter( + **{ + f"{self.pk}__in": [ pk - ).isdigit() # We filter empty values for create views - ] - } - ).all() - ] + for pk in context["widget"]["value"] + if str( + pk + ).isdigit() # We filter empty values for create views + ] + } + ).all() + ) + ).decode("utf-8") return context