モデルのクエリセットを反復処理し、一致するモデルを削除することを目的とした django コードのスニペットがあります。クエリセットが大きくなり、これらのアクションは実際には定期的なタスクに設定されているため、速度が問題になっています。
誰かがそれを最適化するのを手伝ってくれるなら、これがコードです!
# For the below code, "articles" are just django models
all_articles = [a reallly large list of articles]
newest_articles = [some large list of new articles]
unique_articles = []
for new_article in newest_articles:
failed = False
for old_article in all_articles:
# is_similar is just a method which checks if two strings are
# identical to a certain degree
if is_similar(new_article.blurb, old_article.blurb, 0.9)
and is_similar(new_article.title, old_article.title, 0.92):
failed = True
break
if not failed:
unique_articles.append(new_article)
return unique_articles
ありがとうございます!