2

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?

PS: apparently there's a ticket for this use case: https://code.djangoproject.com/ticket/12203

nemesisdesign
  • 8,159
  • 12
  • 58
  • 97
  • Can you describe the user workflow you wish to achieve? Is that something that belongs in the admin (i.e. should you create custom views to prevent end-users from accessing more than they need or make it simpler)? Is that something that could be done using [admin actions](https://docs.djangoproject.com/el/1.10/ref/contrib/admin/actions/) ? – raphv Aug 22 '16 at 10:10
  • @raphv the user interaction is not different than the classic many-to-many admin widget, I just want the user to select the VPNs from the select list checkbox, then the system must create what it needs automatically – nemesisdesign Aug 22 '16 at 11:56

0 Answers0