mirror of
https://github.com/django/django.git
synced 2025-10-24 14:16:09 +00:00
Fixed #21763 -- Added an error msg for missing methods on ManyRelatedManager.
Attempting to add() and remove() an object related by a 'through' model now raises more descriptive AttributeErrors, in line with set and create().
This commit is contained in:
committed by
Tim Graham
parent
29345390b8
commit
12385a5f86
@@ -867,20 +867,29 @@ def create_many_related_manager(superclass, rel):
|
||||
False,
|
||||
self.prefetch_cache_name)
|
||||
|
||||
# If the ManyToMany relation has an intermediary model,
|
||||
# the add and remove methods do not exist.
|
||||
if rel.through._meta.auto_created:
|
||||
def add(self, *objs):
|
||||
self._add_items(self.source_field_name, self.target_field_name, *objs)
|
||||
def add(self, *objs):
|
||||
if not rel.through._meta.auto_created:
|
||||
opts = self.through._meta
|
||||
raise AttributeError(
|
||||
"Cannot use add() on a ManyToManyField which specifies an intermediary model. Use %s.%s's Manager instead." %
|
||||
(opts.app_label, opts.object_name)
|
||||
)
|
||||
self._add_items(self.source_field_name, self.target_field_name, *objs)
|
||||
|
||||
# If this is a symmetrical m2m relation to self, add the mirror entry in the m2m table
|
||||
if self.symmetrical:
|
||||
self._add_items(self.target_field_name, self.source_field_name, *objs)
|
||||
add.alters_data = True
|
||||
# If this is a symmetrical m2m relation to self, add the mirror entry in the m2m table
|
||||
if self.symmetrical:
|
||||
self._add_items(self.target_field_name, self.source_field_name, *objs)
|
||||
add.alters_data = True
|
||||
|
||||
def remove(self, *objs):
|
||||
self._remove_items(self.source_field_name, self.target_field_name, *objs)
|
||||
remove.alters_data = True
|
||||
def remove(self, *objs):
|
||||
if not rel.through._meta.auto_created:
|
||||
opts = self.through._meta
|
||||
raise AttributeError(
|
||||
"Cannot use remove() on a ManyToManyField which specifies an intermediary model. Use %s.%s's Manager instead." %
|
||||
(opts.app_label, opts.object_name)
|
||||
)
|
||||
self._remove_items(self.source_field_name, self.target_field_name, *objs)
|
||||
remove.alters_data = True
|
||||
|
||||
def clear(self):
|
||||
db = router.db_for_write(self.through, instance=self.instance)
|
||||
|
||||
Reference in New Issue
Block a user