Search code examples
djangodjango-modelsmany-to-manydjango-databasedjango-intermediate-table

how to correctly assign the through attribute in django?


I was just looking the documentation to be able to change the intermediate table but when I implement it, I get into trouble:

https://docs.djangoproject.com/en/2.0/topics/db/models/#extra-fields-on-many-to-many-relationships

The problem as such is that, although I can migrate the database and run the application, when I enter the administrator I do not visualize correctly the relationship of my models through the trough attribute (especially a field of my model called Tested).

Why does this happen and how can it be corrected?


Solution

  • This is by design. Django cannot automatically generate the widget for ManyToMany relations that use a through table because of the extra data needed (tested in your case). From Django docs:

    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.

    However, we still want to be able to edit that information inline. Fortunately, this is easy to do with inline admin models.

    Your best bet is to create an inline admin model as explained in the docs.