Skip to content

Commit 3cd1422

Browse files
committed
Added request parameter to ModelSelect2Mixin.result_from_instance() method.
1 parent e8f374c commit 3cd1422

File tree

4 files changed

+8
-5
lines changed

4 files changed

+8
-5
lines changed

django_select2/forms.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -554,17 +554,20 @@ def label_from_instance(obj):
554554
"""
555555
return str(obj)
556556

557-
def result_from_instance(self, obj):
557+
def result_from_instance(self, obj, request):
558558
"""
559559
Return a dictionary representing the object.
560560
561561
Can be overridden to change the result returned by
562562
:class:`.AutoResponseView` for each object.
563563
564+
The request passed in will correspond to the request sent to the
565+
:class:`.AutoResponseView` by the widget.
566+
564567
Example usage::
565568
566569
class MyWidget(ModelSelect2Widget):
567-
def result_from_instance(obj):
570+
def result_from_instance(obj, request):
568571
return {
569572
'id': obj.pk,
570573
'text': self.label_from_instance(obj),

django_select2/views.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def get(self, request, *args, **kwargs):
4444
return JsonResponse(
4545
{
4646
"results": [
47-
self.widget.result_from_instance(obj)
47+
self.widget.result_from_instance(obj, request)
4848
for obj in context["object_list"]
4949
],
5050
"more": context["page_obj"].has_next(),

tests/test_forms.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ def test_result_from_instance_ModelSelect2Widget(self, genres):
442442
widget = ModelSelect2Widget()
443443
widget.model = Genre
444444
genre = Genre.objects.first()
445-
assert widget.result_from_instance(genre) == {
445+
assert widget.result_from_instance(genre, request=None) == {
446446
"id": genre.pk,
447447
"text": str(genre),
448448
}

tests/testapp/forms.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ class CityModelSelect2Widget(ModelSelect2Widget):
238238
model = City
239239
search_fields = ["name"]
240240

241-
def result_from_instance(self, obj):
241+
def result_from_instance(self, obj, request):
242242
return {"id": obj.pk, "text": obj.name, "country": str(obj.country)}
243243

244244

0 commit comments

Comments
 (0)