Django - using Many-to-Many horizontal interface outside of admin

橙三吉。 提交于 2019-12-07 13:43:38

问题


I'm working in a form with a m2m field. I want that this field looks like the horizontal interface of the django admin site... ¿how i can do it?

thanks...


回答1:


You need to use the FilteredSelectMultiple widget

from django.contrib.admin.widgets import FilteredSelectMultiple
from django import forms
from .models import Person


class PersonForm(forms.ModelForm):
    some_field = forms.ModelMultipleChoiceField(Person.objects.all(), widget=FilteredSelectMultiple("Person", False, attrs={'rows':'2'}))
    class Meta:
        model = Person   

You will also need to include the Javascript and CSS used in the admin. Here's an example



来源:https://stackoverflow.com/questions/16203287/django-using-many-to-many-horizontal-interface-outside-of-admin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!