3

Django doesn't support displaying of related objects from a many-to-many relation in the changelist for a good reason. It would result in a lot of database hits.

But sometimes it is inevitable and necessary to e.g. display an object's categories, which have a many-to-many relation to the object, in the changelist. Given that case, does anybody have some experiences/snippets etc. to speed this up a little (thinking of caching, custom sql queries...)? (I am aware of the fact that I can make a method that calls object.categories.all()... But this can really be a pain in the ass...).

Bernhard Vallant
  • 49,468
  • 20
  • 120
  • 148
  • What if you returned a flat values_list with the display value of the related data, and cached that per instance in your parent model? Then wire up a signal to reset these cached values when the parent model is saved. – Brandon Taylor Dec 01 '11 at 17:35

1 Answers1

1

Here you have to make a choice about denormalization in your model if you think that one more database hit per row in your changelist is unacceptable.

The question is how to store this ManyToMany relation ? Maybe you can go with a synced JSON serialized object in a CharField or a TextField to serialize the subset of fields you need (pk and name for instance).

But be careful with the side effects on performances when adding a potentially big column, the queryset's defer method is your friend.

Stan
  • 8,710
  • 2
  • 29
  • 31