18

次の 2 つのモデルが与えられます。

class Item(models.Model):
    timestamp = models.DateTimeField()

class Source(models.Model):
    items = models.ManyToManyField(Item, related_name="sources")

これを使用して、特定の時間前にソースのすべてのアイテムを見つけることができます。

source.items.filter(timestamp__lte=some_datetime)

そのクエリに一致するすべてのアイテムを効率的に削除するにはどうすればよいですか? 私は次のようなことを試すことができると思います:

items_to_remove = list(source.items.filter(timestamp__lte=some_datetime))
source.items.remove(*items_to_remove)

しかし、それは悪いようです。

これらのアイテムは他のソースにも属している可能性があるため、削除したくないことに注意してください。特定のソースとの関係を削除したいだけです。

4

2 に答える 2

34

リストに変換する必要がないことを除いて、あなたはお金でそれを正しく理解したと思います。

source.items.remove(*source.items.filter(*args))

remove/メソッドは次のaddようになります

remove(self, *objs)
add(self, *objs)

そして、ドキュメントは の形式で複数の例を追加するのを使用しているので、引数が同じであることを見て、 に[p1, p2, p3]も同じことが当てはまると思います。remove

>>> a2.publications.add(p1, p2, p3)

もう少し掘り下げて、remove 関数は*objs1 つずつ反復し、有効なモデルであるかどうかをチェックします。そうでない場合は、値を PK として使用してから、 で項目を削除しますpk__in。最初に m2m テーブルにクエリを実行して削除するオブジェクトを探し、次にそれらのオブジェクトを m2m マネージャーに渡します。

    # django.db.models.related.py
    def _remove_items(self, source_field_name, target_field_name, *objs):
        # source_col_name: the PK colname in join_table for the source object
        # target_col_name: the PK colname in join_table for the target object
        # *objs - objects to remove

        # If there aren't any objects, there is nothing to do.
        if objs:
            # Check that all the objects are of the right type
            old_ids = set()
            for obj in objs:
                if isinstance(obj, self.model):
                    old_ids.add(obj.pk)
                else:
                    old_ids.add(obj)
            if self.reverse or source_field_name == self.source_field_name:
                # Don't send the signal when we are deleting the
                # duplicate data row for symmetrical reverse entries.
                signals.m2m_changed.send(sender=rel.through, action="pre_remove",
                    instance=self.instance, reverse=self.reverse,
                    model=self.model, pk_set=old_ids)
            # Remove the specified objects from the join table
            db = router.db_for_write(self.through.__class__, instance=self.instance)
            self.through._default_manager.using(db).filter(**{
                source_field_name: self._pk_val,
                '%s__in' % target_field_name: old_ids
            }).delete()
            if self.reverse or source_field_name == self.source_field_name:
                # Don't send the signal when we are deleting the
                # duplicate data row for symmetrical reverse entries.
                signals.m2m_changed.send(sender=rel.through, action="post_remove",
                    instance=self.instance, reverse=self.reverse,
                    model=self.model, pk_set=old_ids)
于 2011-01-18T01:43:52.247 に答える