Skip to content

How to add property to the select2 options? #143

Answered by rez0n
rez0n asked this question in Q&A
Discussion options

You must be logged in to vote

Actually, I found a solution.
Choices list being generated in the view. So, to pass extra properties to the choices we need to use modified view
Example how to pass test property of the object

from django_select2.views import AutoResponseView
from django.http import JsonResponse

class ServiceSelect2View(AutoResponseView):
    def get(self, request, *args, **kwargs):
        self.widget = self.get_widget_or_404()
        self.term = kwargs.get("term", request.GET.get("term", ""))
        self.object_list = self.get_queryset()
        context = self.get_context_data()
        return JsonResponse(
            {
                "results": [
                    {"text": self.widget.label_from…

Replies: 2 comments

Comment options

rez0n
Jun 8, 2022
Author Sponsor

You must be logged in to vote
0 replies
Comment options

rez0n
Jun 8, 2022
Author Sponsor

You must be logged in to vote
0 replies
Answer selected by rez0n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant