이것은 내가 생각해 낸 것입니다. 대부분의 작업을 수행합니다. 그러나 새 항목을 추가 할 때 목록이 업데이트되지 않고 항목을 변경해도 원래 페이지로 다시 리디렉션되지 않습니다. 당신이 볼 수 있듯이
/your_app/forms.py
class ProductForm(forms.ModelForm):
class Media:
js = ('js/custom_m2m.js',)
class Meta:
model = Product
/your_media/js/custom_m2m.js
django.jQuery(function() {
var $ = django.jQuery;
// Add a new place holder div to hold the m2m list
$('div.options div').append('<div class="newdiv"></div>');
// Assign some variables
var target = "options"; // <-- Target Field
var newdiv = $('div.newdiv');
var next_page = window.location.pathname;
var add_link = $('div.'+target+' div a#add_id_'+target);
var edit_img = "/static/media_admin/img/admin/icon_changelink.gif";
var add_img = "/static/media_admin/img/admin/icon_addlink.gif";
// Make the placeholder div bit nicer
newdiv.attr("style", "line-height:20px; margin-left:105px;");
// Iterate through select options and append them to 'newdiv'
$('select#id_'+target+' option[selected="selected"]').each(function() {
newdiv.append('<a href="/admin/shop/option/'+$(this).val()+'/?next='+next_page+'">'+$(this).text()+' <img src="'+edit_img+'" /></a><br />');
});
// Add a 'Add new' link after the option list
add_link.html('<strong>Add new</strong> ' + add_link.html());
add_link.appendTo(newdiv);
// Show the 'newdiv' and hide the original dropdown
$('select#id_'+target).after(newdiv);
$('select#id_'+target).css("display", "none");
$('div.'+target+' p[class="help"]').css("display", "none");
});
은, 위의 스크립트는 일부 하드 코딩 된 경로를 사용합니다. 어떤 개선이 도움이 될 것입니다.
출처
2010-11-20 06:56:15
Nav