#30231: Field's verbose_name is ignored in FilteredSelectMultiple widget for
inlines created with "Add another" button
-------------------------------------+-------------------------------------
     Reporter:  Florian Schuler      |                    Owner:  Ahisahar
                                     |  Pretel
         Type:  Bug                  |                   Status:  assigned
    Component:  contrib.admin        |                  Version:  2.1
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:  Accepted
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  1
-------------------------------------+-------------------------------------

Comment (by Florian Schuler):

 Replying to [comment:7 Ahisahar Pretel]:
 > I should probably let it to someone who has more experience right
 @Florian Schuler @Tim Graham? This is my first contribution

 Hey @Ahisahar Pretel and @Tim Graham, I just found the origin of this
 wrong behaviour and fixed it in my local Django system (2.1.7)!
 Like already noted in my bug report it seems like a small logic fault in a
 javascript file. In the file which is responsible for initializing the
 FilteredSelectMultiple widget at page load
 (django/contrib/admin/static/admin/js/SelectFilter2.js) there is the
 correct usage of calling the SelectFilter.init() method at the very end
 (line 244 to 250):

 {{{
     window.addEventListener('load', function(e) {
         $('select.selectfilter,
 select.selectfilterstacked').each(function() {
             var $el = $(this),
                 data = $el.data();
             SelectFilter.init($el.attr('id'), data.fieldName,
 parseInt(data.isStacked, 10));
         });
     });
 }}}

 So second parameter has to be the data attribute fieldName of the HTML
 element, which represents the verbose_name if one is given for the field.

 In the js file which is responsible for triggering the initialization of
 newly added FilteredSelectMultiple Widgets after clicking the "add
 another" button (django/contrib/admin/static/admin/js/inlines.js) this
 parameter isn't set correctly.

 **For tabular inlines:**

 Current function:
 {{{
         var updateSelectFilter = function() {
             // If any SelectFilter widgets are a part of the new form,
             // instantiate a new SelectFilter instance for it.
             if (typeof SelectFilter !== 'undefined') {
                 $('.selectfilter').each(function(index, value) {
                     var namearr = value.name.split('-');
                     SelectFilter.init(value.id, namearr[namearr.length -
 1], false);
                 });
                 $('.selectfilterstacked').each(function(index, value) {
                     var namearr = value.name.split('-');
                     SelectFilter.init(value.id, namearr[namearr.length -
 1], true);
                 });
             }
         };
 }}}

 So it gets the field name out of the "name" attribute which is the real
 field name, not the verbose name which could be easily taken with
 "$(this).data('fieldName')"

 Corrected function should be:
 {{{
         var updateSelectFilter = function() {
             // If any SelectFilter widgets are a part of the new form,
             // instantiate a new SelectFilter instance for it.
             if (typeof SelectFilter !== 'undefined') {
                 $('.selectfilter').each(function(index, value) {
                     SelectFilter.init(value.id, $(this).data('fieldName'),
 false);
                 });
                 $('.selectfilterstacked').each(function(index, value) {
                     SelectFilter.init(value.id, $(this).data('fieldName'),
 true);
                 });
             }
         };
 }}}

 **The same for Stacked inline:**

 Current function:
 {{{
  var updateSelectFilter = function() {
             // If any SelectFilter widgets were added, instantiate a new
 instance.
             if (typeof SelectFilter !== "undefined") {
                 $(".selectfilter").each(function(index, value) {
                     var namearr = value.name.split('-');
                     SelectFilter.init(value.id, namearr[namearr.length -
 1], false);
                 });
                 $(".selectfilterstacked").each(function(index, value) {
                     var namearr = value.name.split('-');
                     SelectFilter.init(value.id, namearr[namearr.length -
 1], true);
                 });
             }
         };
 }}}

 Corrected function should be:

 {{{
 var updateSelectFilter = function() {
             // If any SelectFilter widgets were added, instantiate a new
 instance.
             if (typeof SelectFilter !== "undefined") {
                 $(".selectfilter").each(function(index, value) {
                     SelectFilter.init(value.id, $(this).data('fieldName'),
 false);
                 });
                 $(".selectfilterstacked").each(function(index, value) {
                     SelectFilter.init(value.id, $(this).data('fieldName'),
 true);
                 });
             }
         };
 }}}

 At least on my side everything is working correctly with these changes.
 Maybe one of you could recheck my changes and test it on a clean project
 if everything is still working like expected and maybe it would also be a
 good idea to search for more SelectFilter.init() calls at other places in
 the code?! Don't know if it is anywhere else wrongly used.
 It would also be good to check if the data attribute fieldName is
 guaranteed to be set, but because it is used in this way in the
 SelectFilter2.js file i would think so... But i still wonder why it was
 wrongly implemented in the inlines.js file (maybe there was a change
 someday at the generation of the selectfilter elements and the fieldName
 attribute was added later and no one thought about changing it in
 inlines.js file too?!).

 @Ahisahar Pretel or @Tim Graham could any of you take care of checking
 this out and submitting a patch?

-- 
Ticket URL: <https://code.djangoproject.com/ticket/30231#comment:8>
Django <https://code.djangoproject.com/>
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 post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/072.58f00bd845b75d5927d32983d828967c%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to