Skip to content

Commit ece00ff

Browse files
authored
Move create_option functionality into it's own method (yourlabs#744)
Main motivation was wanting to override this behaviour without touching any other parts of render_to_response but I think this is also a clearer way to organise the code. Actually - in most cases all I really want to change is the wording of the create text. How would you feel about a get_create_label(self, queryset) method? (example use case - I if someone enters a string with an @ in it I create a new contact with an email address, otherwise I assume it's a name - I'd like to make this behaviour explicit via the create label)
1 parent 0d7ec6b commit ece00ff

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

src/dal_select2/views.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,9 @@ def get_results(self, context):
2020
} for result in context['object_list']
2121
]
2222

23-
def render_to_response(self, context):
24-
"""Return a JSON response in Select2 format."""
23+
def get_create_option(self, context, q):
24+
"""Form the correct create_option to append to results."""
2525
create_option = []
26-
27-
q = self.request.GET.get('q', None)
28-
2926
display_create_option = False
3027
if self.create_field and q:
3128
page_obj = context.get('page_obj', None)
@@ -38,6 +35,13 @@ def render_to_response(self, context):
3835
'text': _('Create "%(new_value)s"') % {'new_value': q},
3936
'create_id': True,
4037
}]
38+
return create_option
39+
40+
def render_to_response(self, context):
41+
"""Return a JSON response in Select2 format."""
42+
q = self.request.GET.get('q', None)
43+
44+
create_option = self.get_create_option(context, q)
4145

4246
return http.HttpResponse(
4347
json.dumps({

0 commit comments

Comments
 (0)