[Answer]-Django ManyToMany in admin


Short answer: don’t use the admin site.

long answer:
The admin site is great. It’s powerful and neat and easy to configure in a few minutes. But that’s about it. And that’s fine, because it’s not meant to be used for highly costumized projects. Yes, there are some built in ways to create costum forms for the admin site to use instead of the default, or show it in different ways, but it’s still not that flexible

The way I see it, you have two options:

  1. Copy the admin site files to your project, learn how it works and costumize it to your needs

  2. Don’t use the admin site, build your own views.

Now you might seem to like the first idea, but in my experience the second one is always easier. As tempting as it is to use the admin site, it’s not that complicated to built something like that yourself. Here’s a general explanation of how that would look:

url('^myadmin/$', 'myapp.views.main_admin'),
url('^myadmin/(?P<group_pk>\d+)/$', 'myapp.views.admin_devices')

Then you have ‘main_admin’ show a list of groups, and ‘admin_devices’ would show a list of devices and their information in a table:


def admin_devices(request, group_pk)
    group = Group.objects.get(group_pk)
    devices = group.device.all()

    return render_to_response('devices.html', {'devices': devices})


    <th>device name</th>
    <th>host name</th>
    ... and so on...
{% for device in devices %}
        <td>{{ device.device_name }}</td>
        <td>{{ device.host_name }}</td>
        <td>{{ device.ip }}</td>
                    .... and so on...
{% endfor %}

It would look way better than the admin site default look, because you can give each group some image or give a nice UI for all links for the groups, and when you see the device you could avoid loading all 30K devices, and add pagination and some themeing. There are plenty of jQuery packages around that allow easy implementaion of all the things the admin site allows for: column sorting (Datatables) or easy date adding (jQuery UI) and even in-place editing (jEditable) that would make it even easier for your users


Leave a comment