Django m2m through model with classic admin widget

122 views
Skip to first unread message

Federico Capoano

unread,
Aug 22, 2016, 6:00:06 AM8/22/16
to Django users
Hi,

this is a tough question, i've posted it to stackoverflow here:

I include the text of the question here to make it easier to respond:

I need to customise a through model of a many-to-many relationship, the customisation is subtle, because the user won't need do act manually, I try to explain myself better by explaining my use case with the following pseudo code:


RouterConfiguration
- vpn (many-to-many through VpnClient)
# other fields

VpnClient
- router: ForeignKey to RouterConfiguration
- vpn: ForeignKey to Vpn
- cert: ForeignKey to Cert

Vpn
# other fields

Cert
# (stores x509 certificates)
# other fields


The through model VpnClient has only one additional field, a ForeignKey to Cert, but I want VpnClient to automatically create a Cert instance without user interaction and until here there is no problem.

The problem comes in the Django Admin, because as far as I understood, it is not possible to use the classic many2many widget when using a through model:

When you specify an intermediary model using the through argument to a ManyToManyField, the admin will not display a widget by default. This is because each instance of that intermediary model requires more information than could be displayed in a single widget, and the layout required for multiple widgets will vary depending on the intermediate model.

Reference: https://docs.djangoproject.com/en/1.10/ref/contrib/admin/#working-with-many-to-many-intermediary-models

But I don't want the user to insert any extra information. I just want to be able to control the model so it can perform a series of actions automatically.

So my question is: is it possible to have the classic admin widget with a custom through model? If there's no easy solution maybe I could try with a custom widget? Or maybe there is an alternative way to accomplish what I need?


Thanks to anyone who can give me any useful suggestion.

Federico

Tim Graham

unread,
Aug 22, 2016, 8:24:17 AM8/22/16
to Django users
I haven't look into what's needed to make this work on current Django versions, however, if you're interested in contributing to Django, there's an open ticket to allow using the widget as long as there aren't any extra required fields on the through model: https://code.djangoproject.com/ticket/12203

Federico Capoano

unread,
Aug 22, 2016, 9:52:44 AM8/22/16
to Django users
Glad to know there's an accepted ticket for this.

Right now I'm not sure I'll be able to work on it and will probably use a less pretty workaround, but I'm coming to Django Under The Hood and I'm pretty sure I will participate in the sprints, so if I cannot work on it now I already have a ticket to work on by then.

Thanks
Federico
Reply all
Reply to author
Forward
0 new messages