3

Django 不支持在更改列表中显示多对多关系中的相关对象是有充分理由的。这将导致大量的数据库命中。

但有时不可避免且有必要例如在更改列表中显示与对象具有多对多关系的对象类别。鉴于这种情况,是否有人有一些经验/片段等来加快速度(考虑缓存、自定义 sql 查询......)?(我知道我可以制作一个调用的方法object.categories.all()......但这真的很让人头疼......)。

4

1 回答 1

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.

于 2011-12-27T01:10:02.223 回答