2013-02-04 34 views
3

我有一個m2m關係,在我的AdminForm中,我將使用raw_id_fields而不是filter_horizo​​ntal選項。爲了說明,我更喜歡raw_id_fields而不是filter_horizo​​ntal選項,因爲記錄已經被分類。因此,在彈出式窗口中,用戶可以通過類別進行搜索和過濾。 但有兩點我想不通:在raw_id_fields中一次插入更多記錄

  • 可能性在input_field在彈出的窗口
  • 選擇多個記錄顯示真實姓名,而不是PK的

回答

0

最後,我使用的是修改的https://django-salmonella.readthedocs.org/en/latest/。我不顯示輸入字段並在表格中顯示選定的記錄。

+0

我使用django-salmonella來顯示對象的變化字符串值,如何使用它來一次選擇多個記錄? –

0
  1. 這是可能的。爲了選擇一個以上的記錄,你需要通過在Media類的ModelAdmin或窗口小部件的腳本覆蓋默認dismissRelatedLookupPopup()django/contrib/admin/static/admin/js/admin/RelatedObjectLookups.js

    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,你可能想看看他們。

+0

感謝您的回答。這些天我會測試你的提示。 –

+0

我現在開始實施您的輸入。現在我試圖添加標準的彈出窗口(可能只選擇一個recod)到filter_horizo​​ntal小部件。所以實際上,RelatedObjectLookups.js仍然沒有改變。彈出窗口可以正確打開,但是當我選擇一條記錄時窗口不會關閉,而是切換到所選記錄的change_form。我將這個彈出窗口的源代碼與raw_id_fields小部件生成的源代碼進行了比較,它們是相同的。 @okm我無法弄清楚我做錯了什麼。 –