问题
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