@@ -1262,7 +1262,7 @@ def count(self):
12621262else :
12631263return super ().count ()
12641264
1265- def _add_base (self ,* objs ,through_defaults = None ,using = None ):
1265+ def _add_base (self ,* objs ,through_defaults = None ,using = None , raw = False ):
12661266db = using or router .db_for_write (self .through ,instance = self .instance )
12671267with transaction .atomic (using = db ,savepoint = False ):
12681268self ._add_items (
@@ -1271,6 +1271,7 @@ def _add_base(self, *objs, through_defaults=None, using=None):
12711271* objs ,
12721272through_defaults = through_defaults ,
12731273using = db ,
1274+ raw = raw ,
12741275 )
12751276# If this is a symmetrical m2m relation to self, add the mirror
12761277# entry in the m2m table.
@@ -1281,6 +1282,7 @@ def _add_base(self, *objs, through_defaults=None, using=None):
12811282* objs ,
12821283through_defaults = through_defaults ,
12831284using = db ,
1285+ raw = raw ,
12841286 )
12851287
12861288def add (self ,* objs ,through_defaults = None ):
@@ -1297,10 +1299,10 @@ async def aadd(self, *objs, through_defaults=None):
12971299
12981300aadd .alters_data = True
12991301
1300- def _remove_base (self ,* objs ,using = None ):
1302+ def _remove_base (self ,* objs ,using = None , raw = False ):
13011303db = using or router .db_for_write (self .through ,instance = self .instance )
13021304self ._remove_items (
1303- self .source_field_name ,self .target_field_name ,* objs ,using = db
1305+ self .source_field_name ,self .target_field_name ,* objs ,using = db , raw = raw
13041306 )
13051307
13061308def remove (self ,* objs ):
@@ -1315,7 +1317,7 @@ async def aremove(self, *objs):
13151317
13161318aremove .alters_data = True
13171319
1318- def _clear_base (self ,using = None ):
1320+ def _clear_base (self ,using = None , raw = False ):
13191321db = using or router .db_for_write (self .through ,instance = self .instance )
13201322with transaction .atomic (using = db ,savepoint = False ):
13211323signals .m2m_changed .send (
@@ -1326,6 +1328,7 @@ def _clear_base(self, using=None):
13261328model = self .model ,
13271329pk_set = None ,
13281330using = db ,
1331+ raw = raw ,
13291332 )
13301333filters = self ._build_remove_filters (super ().get_queryset ().using (db ))
13311334self .through ._default_manager .using (db ).filter (filters ).delete ()
@@ -1338,6 +1341,7 @@ def _clear_base(self, using=None):
13381341model = self .model ,
13391342pk_set = None ,
13401343using = db ,
1344+ raw = raw ,
13411345 )
13421346
13431347def clear (self ):
@@ -1352,7 +1356,7 @@ async def aclear(self):
13521356
13531357aclear .alters_data = True
13541358
1355- def set_base (self ,objs ,* ,clear = False ,through_defaults = None ):
1359+ def set_base (self ,objs ,* ,clear = False ,through_defaults = None , raw = False ):
13561360# Force evaluation of `objs` in case it's a queryset whose value
13571361# could be affected by `manager.clear()`. Refs #19816.
13581362objs = tuple (objs )
@@ -1361,8 +1365,10 @@ def set_base(self, objs, *, clear=False, through_defaults=None):
13611365with transaction .atomic (using = db ,savepoint = False ):
13621366self ._remove_prefetched_objects ()
13631367if clear :
1364- self ._clear_base (using = db )
1365- self ._add_base (* objs ,through_defaults = through_defaults ,using = db )
1368+ self ._clear_base (using = db ,raw = raw )
1369+ self ._add_base (
1370+ * objs ,through_defaults = through_defaults ,using = db ,raw = raw
1371+ )
13661372else :
13671373old_ids = set (
13681374self .using (db ).values_list (
@@ -1382,9 +1388,9 @@ def set_base(self, objs, *, clear=False, through_defaults=None):
13821388else :
13831389new_objs .append (obj )
13841390
1385- self ._remove_base (* old_ids ,using = db )
1391+ self ._remove_base (* old_ids ,using = db , raw = raw )
13861392self ._add_base (
1387- * new_objs ,through_defaults = through_defaults ,using = db
1393+ * new_objs ,through_defaults = through_defaults ,using = db , raw = raw
13881394 )
13891395
13901396def set (self ,objs ,* ,clear = False ,through_defaults = None ):
@@ -1545,6 +1551,7 @@ def _add_items(
15451551* objs ,
15461552through_defaults = None ,
15471553using = None ,
1554+ raw = False ,
15481555 ):
15491556# source_field_name: the PK fieldname in join table for the source
15501557# object target_field_name: the PK fieldname in join table for the
@@ -1587,6 +1594,7 @@ def _add_items(
15871594model = self .model ,
15881595pk_set = missing_target_ids ,
15891596using = db ,
1597+ raw = raw ,
15901598 )
15911599# Add the ones that aren't there already.
15921600self .through ._default_manager .using (db ).bulk_create (
@@ -1612,10 +1620,11 @@ def _add_items(
16121620model = self .model ,
16131621pk_set = missing_target_ids ,
16141622using = db ,
1623+ raw = raw ,
16151624 )
16161625
16171626def _remove_items (
1618- self ,source_field_name ,target_field_name ,* objs ,using = None
1627+ self ,source_field_name ,target_field_name ,* objs ,using = None , raw = False
16191628 ):
16201629# source_field_name: the PK colname in join table for the source
16211630# object target_field_name: the PK colname in join table for the
@@ -1644,6 +1653,7 @@ def _remove_items(
16441653model = self .model ,
16451654pk_set = old_ids ,
16461655using = db ,
1656+ raw = raw ,
16471657 )
16481658target_model_qs = super ().get_queryset ()
16491659if target_model_qs ._has_filters ():
@@ -1663,6 +1673,7 @@ def _remove_items(
16631673model = self .model ,
16641674pk_set = old_ids ,
16651675using = db ,
1676+ raw = raw ,
16661677 )
16671678
16681679return ManyRelatedManager