Add _none-option to ModelChoiceField and filters for organizer and event-permission in event-typeahead (#6224)

* Add optional filters for organizer and event-permission on event-typeahead

* include _none option only if no search query given

Co-authored-by: luelista <weller@rami.io>

* allow _none in Select2, add ModelChoiceFieldWithNone

* fix flake8

---------

Co-authored-by: luelista <weller@rami.io>
This commit is contained in:
Richard Schreiber
2026-06-02 12:23:25 +02:00
committed by GitHub
parent 375c42dff5
commit d555b23275
4 changed files with 84 additions and 22 deletions

View File

@@ -29,17 +29,30 @@ class Select2Mixin:
super().__init__(*args, **kwargs)
def options(self, name, value, attrs=None):
if value and value[0]:
for i, selected in enumerate(self.choices.queryset.filter(pk__in=value)):
yield self.create_option(
None,
self.choices.field.prepare_value(selected),
self.choices.field.label_from_instance(selected),
True,
i,
subindex=None,
attrs=attrs
)
if not value or not value[0]:
return
has_none = "_none" in value
if has_none:
value = [v for v in value if v != "_none"]
yield self.create_option(
None,
"_none",
self.choices.field.none_label,
True,
0,
subindex=None,
attrs=attrs
)
for i, selected in enumerate(self.choices.queryset.filter(pk__in=value)):
yield self.create_option(
None,
self.choices.field.prepare_value(selected),
self.choices.field.label_from_instance(selected),
True,
i + (1 if has_none else 0),
subindex=None,
attrs=attrs
)
return
def optgroups(self, name, value, attrs=None):