Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango: ManyToMany widget won't be displayed as double list
    text
    copied!<p>Hi Stackoverflow people, </p> <p>I would like to change the m2m widget in the admin pages (and later in the actual django site) to a more user friendly double list (like this <a href="http://blog.micayael.com/wp-content/uploads/2010/12/doublelist.png" rel="nofollow noreferrer">one</a>).</p> <p>I understand that <a href="https://stackoverflow.com/questions/3006401/django-custom-widget-for-manytomany-field">django.contrib.admin.widgets.FilteredSelectMultiple</a> could do this for me. However, I have trouble to get it to work. I have added the code below to my admin.py, but the widget is not changing when I view the model in the admin app.</p> <p>I have tried to adopt the code from <a href="https://stackoverflow.com/questions/660260/django-admin-form-for-many-to-many-relationship">here</a>. Every SupplierProfile should connect to one or more countries from the WorldBorder model (based on the <a href="https://docs.djangoproject.com/en/dev/ref/contrib/gis/tutorial/#worldborders" rel="nofollow noreferrer">GeoDjango example</a>)</p> <p><strong>Where is the flaw in the code? I can't see why it would not be displayed. Thank you for your help!</strong></p> <pre><code>from django import forms from django.contrib.admin.widgets import FilteredSelectMultiple from django.contrib.gis import admin from django.utils.translation import ugettext_lazy as _ from apps.profile.models import (UserProfile, SupplierProfile) from apps.gis_data.models import WorldBorder class WorldBorderAdmin(admin.ModelAdmin): filter_horizonal = ('name',) class SupplierProfileAdminForm(forms.ModelForm): distribution_location_country = forms.ModelMultipleChoiceField( queryset = WorldBorder.objects.all(), required = False, widget = FilteredSelectMultiple( verbose_name = _('Distribution Country'), is_stacked=False ) ) class Meta: model = SupplierProfile def __init__(self, *args, **kwargs): super(SupplierProfileAdminForm, self).__init__(*args, **kwargs) if self.instance: self.fields['distribution_location_country'].initial = self.instance.distribution_location_country.all() def save(self, commit=True): profile = super(SupplierProfileAdminForm, self).save(commit=False) profile.distribution_location_country = self.cleaned_data['distribution_location_country'] if commit: profile.save() profile.save_m2m() return profile class SupplierProfileAdmin(admin.ModelAdmin): form = SupplierProfileAdminForm admin.site.register(SupplierProfile, admin.OSMGeoAdmin) </code></pre> <p><strong>Updated code of admin.py</strong></p> <p>Is it possible to define the double list as stated below? "distribution_location_country" is the m2m field in my SupplierProfile. For some reason, it is still not coming through.</p> <pre><code>from django import forms from django.contrib.admin.widgets import FilteredSelectMultiple from django.contrib.gis import admin class SupplierProfileAdmin(admin.OSMGeoAdmin): filter_horizontal = ('distribution_location_country', ) admin.site.register(SupplierProfile, SupplierProfileAdmin) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload