Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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...

like image 476
Andrés Quiroga Avatar asked Apr 24 '13 22:04

Andrés Quiroga


1 Answers

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

like image 177
msc Avatar answered Sep 18 '22 03:09

msc