                  '%s__in' % self.target_field_name: qs
              })
  
              if self.symmetrical:
                  filters |= Q(**{
                      self.target_field_name: self.related_val,
                      '%s__in' % self.source_field_name: qs
                  })
              return filters
  
          def get_queryset(self):
              try:
                  return self.instance._prefetched_objects_cache[self.prefetch_cache_name]
 
</s> remove         def _build_clear_filters(self, qs):
            filters = Q(**{
                self.source_field_name: self.related_val,
                '%s__in' % self.target_field_name: qs
            })

 </s> add         def _build_remove_filters(self, removed_vals):
            filters = Q(**{self.source_field_name: self.related_val})
            # No need to add a subquery condition if removed_vals is a QuerySet without
            # filters.
            removed_vals_filters = (not isinstance(removed_vals, QuerySet) or
                                    removed_vals._has_filters())
            if removed_vals_filters:
                filters &= Q(**{'%s__in' % self.target_field_name: removed_vals}) </s> remove 
            old_vals_qs = self.using(db).filter(**{
                '%s__in' % self.target_field.related_field.attname: old_ids})
            filters = self._build_clear_filters(old_vals_qs)
 </s> add             target_model_qs = super(ManyRelatedManager, self).get_queryset()
            if target_model_qs._has_filters():
                old_vals = target_model_qs.using(db).filter(**{
                    '%s__in' % self.target_field.related_field.attname: old_ids})
            else:
                old_vals = old_ids
            filters = self._build_remove_filters(old_vals) </s> remove             filters = self._build_clear_filters(self.using(db))
 </s> add             filters = self._build_remove_filters(super(ManyRelatedManager, self).get_queryset().using(db))
                symmetrical_filters = Q(**{self.target_field_name: self.related_val})
                if removed_vals_filters:
                    symmetrical_filters &= Q(
                        **{'%s__in' % self.source_field_name: removed_vals})
                filters |= symmetrical_filters
                  '%s__in' % self.target_field_name: qs
              })
  
              if self.symmetrical:
                symmetrical_filters = Q(**{self.target_field_name: self.related_val})
                if removed_vals_filters:
                    symmetrical_filters &= Q(
                        **{'%s__in' % self.source_field_name: removed_vals})
                filters |= symmetrical_filters
                symmetrical_filters = Q(**{self.target_field_name: self.related_val})
                if removed_vals_filters:
                    symmetrical_filters &= Q(
                        **{'%s__in' % self.source_field_name: removed_vals})
                filters |= symmetrical_filters
                symmetrical_filters = Q(**{self.target_field_name: self.related_val})
                if removed_vals_filters:
                    symmetrical_filters &= Q(
                        **{'%s__in' % self.source_field_name: removed_vals})
                filters |= symmetrical_filters
                symmetrical_filters = Q(**{self.target_field_name: self.related_val})
                if removed_vals_filters:
                    symmetrical_filters &= Q(
                        **{'%s__in' % self.source_field_name: removed_vals})
                filters |= symmetrical_filters
              return filters
  
          def get_queryset(self):
              try:
                  return self.instance._prefetched_objects_cache[self.prefetch_cache_name]
keep keep keep keep replace replace replace replace keep keep keep keep keep
 Used simpler queries for m2m clearing when possible.

Refs #21169 
django/db/models/fields/related.py