[Django]-Insert more records at once in raw_id_fields

0👍

Finally I’m using a modified https://django-salmonella.readthedocs.org/en/latest/. I don’t show the input field and show the selected records in a table.

0👍

  1. It’s possible. In order to select more than one record, you need to override default dismissRelatedLookupPopup() in django/contrib/admin/static/admin/js/admin/RelatedObjectLookups.js by including your script in the Media class of your ModelAdmin or widget:

    var dismissRelatedLookupPopup = (function(prev, $) {
        return function(win, chosenId) {
            var name = windowname_to_id(win.name);
            var elem = document.getElementById(name);
    
            // 1. you could add extra condition checking here, for example
            if ($(elem).hasClass('my_raw_id_ext_cls')) { // add this class to the field
            //     ...logic of inserting picked items from the popup page
            } 
            else { // default logic
                prev(win, chosenId);
            }
    
            // 2. or you could copy the following part from RelatedObjectLookups.js ...
            if (elem.className.indexOf('vManyToManyRawIdAdminField') != -1 && elem.value) {    
                elem.value += ',' + chosenId;
                // 2. and add a return. Remember this acts globally.
                return;
            } else {
                document.getElementById(name).value = chosenId;
            }
    
            // 3. the following line cause the popup to be closed while one item is picked. 
            // You could comment it out, but this would also affect the behavior of picking FK items.
            win.close(); 
    
        }
    })(dismissRelatedLookupPopup, django.jQuery);
    
  2. Django does not support this by default. There are some snippets at djangosnippets.org, you may want to take a look at them.

👤okm

Leave a comment