Insert more records at once into raw_id_fields

I have a m2m relation where in my AdminForm I would use raw_id_fields instead of filter_horizontal option. For the sake of explanation, I prefer raw_id_fields over the filter_horizontal option because the records are already classified. Thus, in the pop-up window, the user can search and filter through the category. But there are two points that I cannot understand:

  • the ability to select more than one entry in a pop-up window
  • showing real names instead of pk in input_field
+3


source to share


2 answers


Finally, I am using the modified https://django-salmonella.readthedocs.org/en/latest/ . I am not showing the input field and showing the selected records in the table.



0


source


  • It is possible. To select more than one entry, you need to override the default dismissRelatedLookupPopup()

    in django/contrib/admin/static/admin/js/admin/RelatedObjectLookups.js

    by including the script in Media

    your class 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);
    
          

  • Django doesn't support this by default. There are several snippets at djangosnippets.org , you can take a look at them.



0


source







All Articles