2.0 Wishlist #58
AlexCLeduc
announced in
Announcements
Replies: 1 comment
-
I would like to start with looking at what an ideal interaction with the API would be (i.e. adding widgets to a form for several use cases such as whole model, filtered based on form instantiation kwargs, filtered based on request user, etc.) Priority 1, IMO:
Secondary but also really important:
Tertiary but easy:
"Do we like how get_items receives values and search?"
Missing items?
Here is the current way I'm using it, with some comments about what I don't like. # "multiselect=True" should be implied when it is in a MultipleChoiceField
self.fields["qa_data_sources"] = forms.ModelMultipleChoiceField(
# The queryset you define is never used, even in the basic usage.
queryset=DataSource.objects.all(),
label=_("Select data source(s)"),
required=False,
widget=Autocomplete(
use_ac=DataSourcesAutocomplete,
attrs={
"component_id": f"id_qa_data_sources", # I shouldn't have to do this
"id": f"id_qa_data_sources__textinput", # I shouldn't have to do this
},
),
)
class DataSourcesAutocomplete(HTMXAutoComplete):
"""Autocomplete component to select Data Sources from a library"""
name = "qa_data_sources"
multiselect = True # This could/should be inferred
# It is temping to set this higher for performance reasons, but we really need lazy loading
minimum_search_length = 0
model = DataSource
def get_items(self, search=None, values=None):
request = get_request() # Relying on a different library for this
# For filtering based on other form fields. Will still have to do something like this,
# but should be made easier on the client side via new Javascript API
library_id = request.GET.get("library_id", None)
if library_id:
library = (
Library.objects.filter(pk=library_id)
.prefetch_related("data_sources")
.first()
)
data = library.data_sources.all()
else:
data = DataSource.objects.all()
if search is not None:
items = [
{"label": str(x), "value": str(x.id)}
for x in data
# Refactor so equality comparison is a function passed in?
if search == "" or str(search).upper() in f"{x}".upper()
]
return items
if values is not None: # I don't really understand what this is for.
items = [
{"label": str(x), "value": str(x.id)}
for x in data
if str(x.id) in values
]
return items
return [] |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
So I've been familiarizing myself with this codebase, wrote a bunch of tests, and I'm now ready to break everything and put it back together under a new 2.0 API. What should be the priorities?
class Meta
API and bad class-based practices__subclasses__
, this is another thing that'll have to change{% autocomplete %}
template helper, but I don't think we need that eitherget_items
methodPermissionDenied
orBadRequest
, there's currently no way of protecting autocomplete routes without resorting to middlewareMustBeLoggedInAutocomplete
MixinProjectForm
and want to filter available tag choices based on the project, you're modifying.Beta Was this translation helpful? Give feedback.
All reactions