Re: [Django] #32993: Refactor AutocompleteJsonView to support extra fields in autocomplete response

2021-08-10 Thread Django
#32993: Refactor AutocompleteJsonView to support extra fields in autocomplete
response
-+-
 Reporter:  mrts |Owner:  mrts
 Type:   |   Status:  closed
  Cleanup/optimization   |
Component:  contrib.admin|  Version:  dev
 Severity:  Normal   |   Resolution:  fixed
 Keywords:   | Triage Stage:  Ready for
 |  checkin
Has patch:  1|  Needs documentation:  0
  Needs tests:  0|  Patch needs improvement:  0
Easy pickings:  1|UI/UX:  0
-+-
Changes (by Mariusz Felisiak ):

 * status:  assigned => closed
 * resolution:   => fixed


Comment:

 In [changeset:"9b1158a7e0784686bbe5118a88d4804b99fa4fe1" 9b1158a7]:
 {{{
 #!CommitTicketReference repository=""
 revision="9b1158a7e0784686bbe5118a88d4804b99fa4fe1"
 Fixed #32993 -- Added AutocompleteJsonView.serialize_result() to allow
 customization.
 }}}

-- 
Ticket URL: 
Django 
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/062.0eb6558816f2f3441cc0faca09078006%40djangoproject.com.


Re: [Django] #32993: Refactor AutocompleteJsonView to support extra fields in autocomplete response

2021-08-09 Thread Django
#32993: Refactor AutocompleteJsonView to support extra fields in autocomplete
response
-+-
 Reporter:  mrts |Owner:  mrts
 Type:   |   Status:  assigned
  Cleanup/optimization   |
Component:  contrib.admin|  Version:  dev
 Severity:  Normal   |   Resolution:
 Keywords:   | Triage Stage:  Ready for
 |  checkin
Has patch:  1|  Needs documentation:  0
  Needs tests:  0|  Patch needs improvement:  0
Easy pickings:  1|UI/UX:  0
-+-
Changes (by Mariusz Felisiak):

 * needs_tests:  1 => 0
 * stage:  Accepted => Ready for checkin


-- 
Ticket URL: 
Django 
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/062.ccdf20e83b0791e1912e85bbe2a41f22%40djangoproject.com.


Re: [Django] #32993: Refactor AutocompleteJsonView to support extra fields in autocomplete response

2021-08-08 Thread Django
#32993: Refactor AutocompleteJsonView to support extra fields in autocomplete
response
--+
 Reporter:  mrts  |Owner:  mrts
 Type:  Cleanup/optimization  |   Status:  assigned
Component:  contrib.admin |  Version:  dev
 Severity:  Normal|   Resolution:
 Keywords:| Triage Stage:  Accepted
Has patch:  1 |  Needs documentation:  0
  Needs tests:  1 |  Patch needs improvement:  0
Easy pickings:  1 |UI/UX:  0
--+
Changes (by Mariusz Felisiak):

 * needs_tests:  0 => 1


-- 
Ticket URL: 
Django 
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/062.a8af1b9ac03628a5b7b0370d12491cab%40djangoproject.com.


Re: [Django] #32993: Refactor AutocompleteJsonView to support extra fields in autocomplete response

2021-08-08 Thread Django
#32993: Refactor AutocompleteJsonView to support extra fields in autocomplete
response
--+
 Reporter:  mrts  |Owner:  mrts
 Type:  Cleanup/optimization  |   Status:  assigned
Component:  contrib.admin |  Version:  dev
 Severity:  Normal|   Resolution:
 Keywords:| Triage Stage:  Accepted
Has patch:  1 |  Needs documentation:  0
  Needs tests:  0 |  Patch needs improvement:  0
Easy pickings:  1 |UI/UX:  0
--+
Changes (by Mariusz Felisiak):

 * type:  New feature => Cleanup/optimization


-- 
Ticket URL: 
Django 
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/062.b1a15d0b765c78e1bba49105dfcf0d20%40djangoproject.com.


Re: [Django] #32993: Refactor AutocompleteJsonView to support extra fields in autocomplete response

2021-08-08 Thread Django
#32993: Refactor AutocompleteJsonView to support extra fields in autocomplete
response
---+
 Reporter:  mrts   |Owner:  mrts
 Type:  New feature|   Status:  assigned
Component:  contrib.admin  |  Version:  dev
 Severity:  Normal |   Resolution:
 Keywords: | Triage Stage:  Accepted
Has patch:  1  |  Needs documentation:  0
  Needs tests:  0  |  Patch needs improvement:  0
Easy pickings:  1  |UI/UX:  0
---+
Description changed by mrts:

Old description:

> Adding data attributes to items in ordinary non-autocomplete foreign key
> fields that use {{{forms.widgets.Select}}}-based widgets is relatively
> easy. This enables powerful and dynamic admin site customizations where
> fields from related models are updated immediately when users change the
> selected item.
>
> However, adding new attributes to autocomplete field results currently
> requires extending
> {{{contrib.admin.views.autocomplete.AutocompleteJsonView}}} and fully
> overriding the {{{AutocompleteJsonView.get()}}} method. Here's an
> example:
>
> {{{#!python
> class MyModelAdmin(admin.ModelAdmin):
> def get_urls(self):
> return [
> path('autocomplete/',
> CustomAutocompleteJsonView.as_view(admin_site=self.admin_site))
> if url.pattern.match('autocomplete/')
> else url for url in super().get_urls()
> ]
>
> class CustomAutocompleteJsonView(AutocompleteJsonView):
>
> def get(self, request, *args, **kwargs):
> self.term, self.model_admin, self.source_field, to_field_name =
> self.process_request(request)
>
> if not self.has_perm(request):
> raise PermissionDenied
>
> self.object_list = self.get_queryset()
> context = self.get_context_data()
> return JsonResponse({
> 'results': [
> {'id': str(getattr(obj, to_field_name)), 'text':
> str(obj), 'notes': obj.notes} # <-- customization here
> for obj in context['object_list']
> ],
> 'pagination': {'more': context['page_obj'].has_next()},
> })
> }}}
>

> The problem with this is that as {{{AutocompleteJsonView.get()}}} keeps
> evolving, there's quite a lot of maintenance overhead required to catch
> up.
>
> The solutions is simple, side-effect- and risk-free: adding a result
> customization extension point to {{{get()}}} by moving the lines that
> construct the results inside {{{JsonResponse}}} constructor to a separate
> method. So instead of
>
> {{{#!python
> return JsonResponse({
> 'results': [
> {'id': str(getattr(obj, to_field_name)), 'text':
> str(obj)}
> for obj in context['object_list']
> ],
> 'pagination': {'more': context['page_obj'].has_next()},
> })
> }}}
>
> there would be
>
> {{{#!python
> return JsonResponse({
> 'results': [
> self.obj_to_dict(obj, to_field_name) for obj in
> context['object_list']
> ],
> 'pagination': {'more': context['page_obj'].has_next()},
> })
> }}}
>
> where {{{obj_to_dict()}}} contains the original object to dictionary
> conversion code that would be now easy to override:
>
> {{{#!python
> def obj_to_dict(self, obj, to_field_name):
> return {'id': str(getattr(obj, to_field_name)), 'text': str(obj)}
> }}}
>
> The example {{{CustomAutocompleteJsonView}}} from above would now become
> succinct and maintainable:
>
> {{{#!python
> class CustomAutocompleteJsonView(AutocompleteJsonView):
>
> def obj_to_dict(self, obj, to_field_name):
> return super.obj_to_dict(obj, to_field_name) | {'notes':
> obj.notes}
> }}}
>
> What do you think, is this acceptable? I'm more than happy to provide the
> patch.

New description:

 Adding data attributes to items in ordinary non-autocomplete foreign key
 fields that use {{{forms.widgets.Select}}}-based widgets is relatively
 easy. This enables powerful and dynamic admin site customizations where
 fields from related models are updated immediately when users change the
 selected item.

 However, adding new attributes to autocomplete field results currently
 requires extending
 {{{contrib.admin.views.autocomplete.AutocompleteJsonView}}} and fully
 overriding the {{{AutocompleteJsonView.get()}}} method. Here's an example:

 {{{#!python
 class MyModelAdmin(admin.ModelAdmin):
 def get_urls(self):
 return [
 path('autocomplete/',
 CustomAutocompleteJsonView.as_view(admin_site=self.admin_site))
 if url.pattern.match('autocomplete/')
 else url for url in super().get_urls()
 ]

 class 

Re: [Django] #32993: Refactor AutocompleteJsonView to support extra fields in autocomplete response

2021-08-08 Thread Django
#32993: Refactor AutocompleteJsonView to support extra fields in autocomplete
response
---+
 Reporter:  mrts   |Owner:  mrts
 Type:  New feature|   Status:  assigned
Component:  contrib.admin  |  Version:  dev
 Severity:  Normal |   Resolution:
 Keywords: | Triage Stage:  Accepted
Has patch:  1  |  Needs documentation:  0
  Needs tests:  0  |  Patch needs improvement:  0
Easy pickings:  1  |UI/UX:  0
---+

Comment (by mrts):

 Thanks for the heads up! Will keep it in mind in the future.

-- 
Ticket URL: 
Django 
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/062.7151929f59b6de4d4e97f978e055e629%40djangoproject.com.


Re: [Django] #32993: Refactor AutocompleteJsonView to support extra fields in autocomplete response

2021-08-07 Thread Django
#32993: Refactor AutocompleteJsonView to support extra fields in autocomplete
response
---+
 Reporter:  mrts   |Owner:  mrts
 Type:  New feature|   Status:  assigned
Component:  contrib.admin  |  Version:  dev
 Severity:  Normal |   Resolution:
 Keywords: | Triage Stage:  Accepted
Has patch:  1  |  Needs documentation:  0
  Needs tests:  0  |  Patch needs improvement:  0
Easy pickings:  1  |UI/UX:  0
---+
Changes (by Jacob Walls):

 * owner:  (none) => mrts
 * status:  new => assigned


Comment:

 Just a small note for future: you can keep the ticket assigned to yourself
 while you're actively maintaining a PR for it.

-- 
Ticket URL: 
Django 
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/062.1ae1be9d07bf2cd83b835d2123541d18%40djangoproject.com.


Re: [Django] #32993: Refactor AutocompleteJsonView to support extra fields in autocomplete response

2021-08-07 Thread Django
#32993: Refactor AutocompleteJsonView to support extra fields in autocomplete
response
---+
 Reporter:  mrts   |Owner:  (none)
 Type:  New feature|   Status:  new
Component:  contrib.admin  |  Version:  dev
 Severity:  Normal |   Resolution:
 Keywords: | Triage Stage:  Accepted
Has patch:  1  |  Needs documentation:  0
  Needs tests:  0  |  Patch needs improvement:  0
Easy pickings:  1  |UI/UX:  0
---+
Changes (by mrts):

 * owner:  mrts => (none)
 * status:  assigned => new
 * has_patch:  0 => 1


Comment:

 [https://github.com/django/django/pull/14752/files PR]

-- 
Ticket URL: 
Django 
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/062.cf089a7191f0fa3b2ba196e5b7ec9f5c%40djangoproject.com.


Re: [Django] #32993: Refactor AutocompleteJsonView to support extra fields in autocomplete response

2021-08-07 Thread Django
#32993: Refactor AutocompleteJsonView to support extra fields in autocomplete
response
---+
 Reporter:  mrts   |Owner:  mrts
 Type:  New feature|   Status:  assigned
Component:  contrib.admin  |  Version:  dev
 Severity:  Normal |   Resolution:
 Keywords: | Triage Stage:  Accepted
Has patch:  0  |  Needs documentation:  0
  Needs tests:  0  |  Patch needs improvement:  0
Easy pickings:  1  |UI/UX:  0
---+
Changes (by mrts):

 * owner:  nobody => mrts
 * status:  new => assigned


-- 
Ticket URL: 
Django 
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/062.cc09ae5881b8f6435c1803c814594eb9%40djangoproject.com.


Re: [Django] #32993: Refactor AutocompleteJsonView to support extra fields in autocomplete response

2021-08-06 Thread Django
#32993: Refactor AutocompleteJsonView to support extra fields in autocomplete
response
---+
 Reporter:  mrts   |Owner:  nobody
 Type:  New feature|   Status:  new
Component:  contrib.admin  |  Version:  dev
 Severity:  Normal |   Resolution:
 Keywords: | Triage Stage:  Accepted
Has patch:  0  |  Needs documentation:  0
  Needs tests:  0  |  Patch needs improvement:  0
Easy pickings:  1  |UI/UX:  0
---+
Changes (by Mariusz Felisiak):

 * type:  Cleanup/optimization => New feature


-- 
Ticket URL: 
Django 
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/062.ffc0246f87796d07ed77b3e723a1ae3b%40djangoproject.com.


Re: [Django] #32993: Refactor AutocompleteJsonView to support extra fields in autocomplete response

2021-08-06 Thread Django
#32993: Refactor AutocompleteJsonView to support extra fields in autocomplete
response
--+
 Reporter:  mrts  |Owner:  nobody
 Type:  Cleanup/optimization  |   Status:  new
Component:  contrib.admin |  Version:  dev
 Severity:  Normal|   Resolution:
 Keywords:| Triage Stage:  Accepted
Has patch:  0 |  Needs documentation:  0
  Needs tests:  0 |  Patch needs improvement:  0
Easy pickings:  1 |UI/UX:  0
--+
Changes (by Claude Paroz):

 * version:  3.2 => dev
 * stage:  Unreviewed => Accepted


Comment:

 Makes sense to me.

-- 
Ticket URL: 
Django 
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/062.a07b0b023035ab3a3e6c9336019baf70%40djangoproject.com.


Re: [Django] #32993: Refactor AutocompleteJsonView to support extra fields in autocomplete response

2021-08-06 Thread Django
#32993: Refactor AutocompleteJsonView to support extra fields in autocomplete
response
-+-
 Reporter:  mrts |Owner:  nobody
 Type:   |   Status:  new
  Cleanup/optimization   |
Component:  contrib.admin|  Version:  3.2
 Severity:  Normal   |   Resolution:
 Keywords:   | Triage Stage:
 |  Unreviewed
Has patch:  0|  Needs documentation:  0
  Needs tests:  0|  Patch needs improvement:  0
Easy pickings:  1|UI/UX:  0
-+-
Description changed by mrts:

Old description:

> Adding data attributes to items in ordinary non-autocomplete foreign key
> fields that use {{{forms.widgets.Select}}}-based widgets is relatively
> easy. This enables powerful and dynamic admin site customizations where
> fields from related models are updated immediately when users change the
> selected item.
>
> However, adding new attributes to autocomplete field results currently
> requires extending
> {{{contrib.admin.views.autocomplete.AutocompleteJsonView}}} and fully
> overriding the {{{AutocompleteJsonView.get()}}} method. Here's an
> example:
>
> {{{#!python
> class MyModelAdmin(admin.ModelAdmin):
> def get_urls(self):
> return [
> path('autocomplete/',
> CustomAutocompleteJsonView.as_view(admin_site=self.admin_site))
> if url.pattern.match('autocomplete/')
> else url for url in super().get_urls()
> ]
>
> class CustomAutocompleteJsonView(AutocompleteJsonView):
>
> def get(self, request, *args, **kwargs):
> self.term, self.model_admin, self.source_field, to_field_name =
> self.process_request(request)
>
> if not self.has_perm(request):
> raise PermissionDenied
>
> self.object_list = self.get_queryset()
> context = self.get_context_data()
> return JsonResponse({
> 'results': [
> {'id': str(getattr(obj, to_field_name)), 'text':
> str(obj), 'notes': obj.notes} # <-- customization here
> for obj in context['object_list']
> ],
> 'pagination': {'more': context['page_obj'].has_next()},
> })
> }}}
>

> The problem with this is that as {{{AutocompleteJsonView.get()}}} keeps
> evolving, there's quite a lot of maintenance overhead required to catch
> up.
>
> The solutions is simple, side-effect- and risk-free: adding a result
> customization extension point to {{{get()}}} by moving the lines that
> construct the results inside {{{JsonResponse}}} to a separate method. So
> instead of
>
> {{{#!python
> return JsonResponse({
> 'results': [
> {'id': str(getattr(obj, to_field_name)), 'text':
> str(obj)}
> for obj in context['object_list']
> ],
> 'pagination': {'more': context['page_obj'].has_next()},
> })
> }}}
>
> there would be
>
> {{{#!python
> return JsonResponse({
> 'results': [
> self.obj_to_dict(obj, to_field_name) for obj in
> context['object_list']
> ],
> 'pagination': {'more': context['page_obj'].has_next()},
> })
> }}}
>
> where {{{obj_to_dict()}}} contains the original object to dictionary
> conversion code that would be now easy to override:
>
> {{{#!python
> def obj_to_dict(self, obj, to_field_name):
> return {'id': str(getattr(obj, to_field_name)), 'text': str(obj)}
> }}}
>
> The example {{{CustomAutocompleteJsonView}}} from above would now become
> succinct and maintainable:
>
> {{{#!python
> class CustomAutocompleteJsonView(AutocompleteJsonView):
>
> def obj_to_dict(self, obj, to_field_name):
> return super.obj_to_dict(obj, to_field_name) | {'notes':
> obj.notes}
> }}}
>
> What do you think, is this acceptable? I'm more than happy to provide the
> patch.

New description:

 Adding data attributes to items in ordinary non-autocomplete foreign key
 fields that use {{{forms.widgets.Select}}}-based widgets is relatively
 easy. This enables powerful and dynamic admin site customizations where
 fields from related models are updated immediately when users change the
 selected item.

 However, adding new attributes to autocomplete field results currently
 requires extending
 {{{contrib.admin.views.autocomplete.AutocompleteJsonView}}} and fully
 overriding the {{{AutocompleteJsonView.get()}}} method. Here's an example:

 {{{#!python
 class MyModelAdmin(admin.ModelAdmin):
 def get_urls(self):
 return [
 path('autocomplete/',
 CustomAutocompleteJsonView.as_view(admin_site=self.admin_site))
 if