参考URL:
http://www.hoboes.com/Mimsy/hacks/replicating-djangos-admin/reusing-djangos-filter_horizontal/
http://www.hoboes.com/Mimsy/hacks/replicating-djangos-admin/
我要想的东东,没有上面讲的那么复杂,就是想在用户操作M2M时,可以从容的选择不同的选项,而不用担心哪些东东被漏了。
按上面的文档,我的实现如下:
一,新建一个模板文档,将相关的DJANGO管理里的CSS,JS移出来备用。
popupplus.html(注意初始化的参数,0为横向,1为纵向)
{% load staticfiles %} <link rel="stylesheet" href={% static "admin/css/widgets.css"%} /> <script src={% static "/admin/jsi18n/"%}></script> <script src={% static "admin/js/core.js"%}></script> <script src={% static "admin/js/SelectBox.js"%}></script> <script src={% static "admin/js/SelectFilter2.js"%}></script> {% if multiple %} <script type="text/javascript"> addEvent(window, "load", function(e) { SelectFilter.init("id_{{ field }}", "{{ field }}", 0, "/static/admin/"); }); </script> {% endif %}
二,重写forms.py里的代码,让其继承forms.SelectMultiple来改写:
class MultipleSelectWithPop(forms.SelectMultiple): def render(self, name, *args, **kwargs): html = super(MultipleSelectWithPop, self).render(name, *args, **kwargs) popupplus = render_to_string("rightmanage/popupplus.html", {'field': name, 'multiple': True}) return html+popupplus
三,让相关字段继承这个类,来实现横向选择。
class RightManageFATForm(forms.ModelForm): def __init__(self, *args, **kwargs): super(RightManageFATForm, self).__init__(*args, **kwargs) fat_users = forms.ModelMultipleChoiceField( User.objects, widget=MultipleSelectWithPop, ) name = forms.CharField( required=False, error_messages={'required': "不能为空"}, label=u"项目名称", widget=forms.TextInput( attrs={ 'class': 'uk-width-1-4', 'disabled': 'true', } ), ) class Meta: model = Site fields = ['name', 'fat_users', ] exclude = ['app_name', 'description', 'app_name', 'manage_user', 'uat_users', 'prd_users', 'valid_users', ]
四,然后,,好像就抽定啦。。。:)好像字体没调好,暂时不管。。。