yourlabs / django-autocomplete-light

A fresh approach to autocomplete implementations, specially for Django. Status: v4 alpha, v3 stable, v2 & v1 deprecated.
https://django-autocomplete-light.readthedocs.io
MIT License
1.81k stars 468 forks source link

Select2GroupQuerySetView and 'descricao' #1278

Closed Rastopapola closed 2 years ago

Rastopapola commented 2 years ago

django-autocomplete-light 3.8.2

Hi everyone,

first of all: I love your work on this package so much! It makes my life - and surely that of a lot of other people - a lot easier!

Now let's get to the real stuff.

Background

I always used Select2QuerySetView as my default base class for different autocomplete functionalities. In my current project, I need to provide parent-child info of selectable data, so a user is able to see directly which entry belongs to which parent topic. Straight forward.

Select2GroupQuerySetView

Since I knew you are using Select2 and the optgroup html tag provides grouping of choices as well, there had to be a grouping view somewhere. Since this case might be a little more special, I couldn't find it on the documentation, but succeeded on your source code.

So far so good. But there is one thing, that is not explained on the source code and neither on the documentation. It's this part, that leaves me head scratching:


class Select2GroupQuerySetView(Select2QuerySetView):
    """List of grouped options for a Select2 widget.

    .. py:attribute:: group_by_related

        Name of the field for the related Model on a One to Many relation

    .. py:attribute:: related_field_name

        Name of the related Model field to run filter against.
    """

    group_by_related = None
    related_field_name = 'name'

    def get_results(self, context):
        """Return the options grouped by a common related model.

        Raises ImproperlyConfigured if self.group_by_name is not configured
        """
       ...

        return [{
            'id': None,
            'text': group,
            'children': [{
                'id': result.id,
                'text': getattr(result, self.related_field_name),
                'title': result.descricao
            } for result in results]
        } for group, results in groups.items()]

Who and what is descricao? Since this attribute is mandatory on the provided choice models, I had to workaround like this

class AutocompleteModel:
    ...
    @property
    def descricao(self):
        return None

Question

What is descricao and where can I find more information on this in the docs?

Thanks a lot!

Rastopapola commented 2 years ago

Update

I just discovered the following issue and related pull request, which is the origin of the above mentioned code snippet https://github.com/yourlabs/django-autocomplete-light/pull/1186

It looks like this has been changed on a recent version of dal which is not installed by default as the most current version by pip: https://github.com/yourlabs/django-autocomplete-light/issues/1267

@jpic If I see this correctly, an update on at least 3.9.0-rc1 would be required to solve this whole issue?

jpic commented 2 years ago

It should have been str(result) indeed!

Should be fixed in 3.9.1 thanks to @OmenApps