Django には、次のような多対多の関係があります。
class Account ( models.Model ):
name = models.CharField( max_length = 30 )
user = models.ForeignKey( User, null = True, blank = True )
class Publisher ( models.Model ):
publisherID = models.BigIntegerField( )
name = models.CharField( max_length = 30 )
lastRequestedId = models.BigIntegerField( )
class Subscription ( models.Model ):
user = models.ForeignKey( Account )
twitterPublisher = models.ForeignKey( Publisher )
アカウントの publisherID のリストはサブスクリプション リストです。ときどき、すべてのアカウントのサブスクリプションをそのような publisherID のリストでスキャンしてサブスクリプションを更新し、アカウントのサブスクリプションに見つからない publisherID の新しいサブスクリプションを追加し、アカウントの publisherID のリストに見つからないサブスクリプションを削除したいと思います。このリストは他の場所で取得されます。
これを行う最善の方法は何ですか?Subscription.objects.all( ) を実行し、それぞれをループして (多数になる可能性があります!)、サブスクリプションとユーザーを追跡し、データベースの現在の状態と新しい情報を理解したら、最終的に更新しますか? それとも、アカウントをループして、Subscription オブジェクトで filter( ) を使用しますか? それは多くのmysqlクエリにつながりますか?
取得した publisherID のリストは次のようになります。
[15446531, 15503880, 4909151, 105263800, 21457289, 14293310, 807095, 14511951,
20032100, 20214495, 15234407, 5988062, 16120265, 50393960, 33933259, 10059852, 652193,
88992966, 43166813, 65682198, 14581921, 12044602, 752673, 14414230, 18994120, 17180567,
17633960, 1468401, 71876190, 23969777, 63490206, 2425151, 14456474, 18396070, 62205298,
11348282, 62581962, 15234886, 30313925, 15166438, 17525171, 15007149, 10760422, 22677790,
14874480, 22093112, 24741685, 10454572, 19015586, 14572071, 37492156, 6253282, 37996645,
18725061, 15983724, 8294212, 24431892, 14589771, 773287, 20772763, 22636391, 816653,
19394188, 49793, 1688, 15813140, 20536157, 202003, 685513, 1000591, 17220934, 972651,
5668942, 1692901, 15913]
明確化:アカウントごとの ID のリストがあります。また、ユーザーがサブスクリプションを持っている場合、そのユーザーのパブリッシャー ID のリストにない場合、サブスクリプションを削除したいと考えています。