私はdjangoで次のモデル構造を持っています。単一クエリのManyToManyフィールドをdjangoモデルで取得する最適化方法
Category:
- name
- description
Tag:
- name
Article:
- guid
- title
- detail
- tags (Many2Many with Tag)
- cats (Many2Many with Category)
私は一括(最大100,000)の記事更新ジョブで、次の操作を実行する必要があります。
- for each article:
- to check if this article has already appeared in another category
- if yes, associate article with current category
- if no, create article along current category
- to check if any of associated tags are exists or not,
- if not exists create tag, and associate with current article
- if exists, just associated with current article
この操作を実行中に、データベースからフェッチされた既存の記事の辞書が既にあります。
ManyToManyフィールドはdjangoのselect_related()のようなフェッチ操作に影響しないので、カテゴリやタグとともに既存の記事を読み込むのに役立つ方法はありますか?