4

我有一个 m2m 关系,在我的 AdminForm 中我会使用 raw_id_fields 而不是 filter_horizo​​ntal 选项。为了解释,我更喜欢 raw_id_fields 而不是 filter_horizo​​ntal 选项,因为记录已经分类。因此,在弹出窗口中,用户可以通过类别进行搜索和过滤。但是有两点我想不通:

  • 在弹出窗口中选择多个记录的可能性
  • 在 input_field 中显示真实姓名而不是 pk
4

2 回答 2

0
  1. 这是可能的。为了选择多个记录,您需要通过将脚本包含在您的或小部件的类中dismissRelatedLookupPopup()来覆盖默认值:django/contrib/admin/static/admin/js/admin/RelatedObjectLookups.jsMediaModelAdmin

    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 默认不支持这个。djangosnippets.org上有一些片段,你可能想看看它们。

于 2013-02-04T14:47:21.593 回答
0

最后,我使用了修改后的https://django-salmonella.readthedocs.org/en/latest/。我不显示输入字段并在表格中显示选定的记录。

于 2013-06-05T06:59:53.233 回答