Django: displaying many-to-many fields in a change list

Django does not support displaying many-to-many related objects in a change list for a good reason. This will lead to a large number of hits in the database.

But sometimes this is inevitable and necessary, for example, to display the categories of objects that have a many-to-many relationship to the object in the list of changes. Given this case, does anyone have some impressions / snippets, etc., to speed things up a bit (thinking of caching, custom SQL queries ...)? (I know that I can make a method that calls object.categories.all()... But it could be a pain in the ass ...).

+3
source share
1 answer

Here you have to make a choice in denormalizationyour model if you think that another blow to the database in a row in your list of changes is unacceptable.

The question is how to maintain this ManyToMany relationship? Perhaps you can go with a synchronized JSONserialized object in CharFieldor TextFieldto serialize a subset of the fields ( pkand name) you need .

But be careful with side effects on performance when adding a potentially large column, requesting a deferral method is your friend.

+1

Source: https://habr.com/ru/post/1767897/


All Articles