how to resize django ManyToManyField widget on admin page
<p>I'm trying to resize a many to many field of django 1.8 admin.</p>

<p>models.py</p>

<pre><code>from django.db import models

class Material(models.Model):
    type = models.CharField(max_length=20, primary_key=True)
    def __unicode__(self):
        return self.type


class Pen(models.Model):
    label = models.CharField(max_length=20, blank=True, default='')
    materials = models.ManyToManyField(Material)
</code></pre>

<p>admin.py</p>

<pre><code>from django.contrib import admin
from django.conf.urls import url
from .models import *
from django.forms import SelectMultiple

@admin.register(Pen)
class PenAdmin(admin.ModelAdmin):
    filter_horizontal = ('materials',)
    #formfield_overrides = { models.ManyToManyField: {'widget': SelectMultiple(attrs={'size':'5', 'width': '50px', 'style': 'width:50px'})}, }

    def get_form(self, request, obj=None, **kwargs):
        form = super(PenAdmin, self).get_form(request, obj, **kwargs)
        form.base_fields['materials'].widget.attrs['style'] = 'width: 70px;'
        return form

    class Media:
        js = ("js/resize_fields.js",)

@admin.register(Material)
class MaterialAdmin(admin.ModelAdmin):
    pass
</code></pre>

<p>resize_fields.js</p>

<pre><code>document.onload = function() {
  document.getElementById("id_materials_to").style.width="70px";
};
</code></pre>

<p>As you can see I tried 3 approaches:</p>

<p>1: using formfield_overrides. It did not make any difference.</p>

<p>2: loading a javascript to resize the html object. It's firing up but not resizing it because the script seems to run before the widget is loaded.</p>

<p>3: using get_form. As shown below, it only resizes one of the select boxes.</p>

<p><a href="https://i.stack.imgur.com/dtXg5.png" rel="noreferrer"><img src="https://i.stack.imgur.com/dtXg5.png" alt="enter image description here"></a></p>

<p>Does anyone know how to resize it horizontally?</p>

<p><strong>UPDATE:</strong>
I discovered that:</p>

<pre><code>class PenAdmin(admin.ModelAdmin):
    formfield_overrides = {
        models.ManyToManyField: {'widget': SelectMultiple(attrs={'size':'5', 'style': 'color:blue;width:250px'})},

    }
</code></pre>

<p>would work since I removed filter_horizontal but I want to use filter_horizontal!
If I'm not mistaken, the filter_horizontal option will use the MultiWidget which is apparently not passing the override attributes to sub-widgets.
Still, I think there must be a solution where I can run a javascript after the page load.
plz help</p>