1
0
mirror of https://github.com/django/django.git synced 2024-12-22 17:16:24 +00:00

Refs #25550 -- Corrected deprecation message for assigning M2M relations.

This commit is contained in:
Tim Graham 2016-07-30 20:50:09 -04:00 committed by GitHub
parent 4e86168290
commit 5fa4370543
2 changed files with 26 additions and 5 deletions

View File

@ -509,6 +509,12 @@ class ReverseManyToOneDescriptor(object):
return self.related_manager_cls(instance) return self.related_manager_cls(instance)
def _get_set_deprecation_msg_params(self):
return ( # RemovedInDjango20Warning
'reverse side of a related set',
self.rel.get_accessor_name(),
)
def __set__(self, instance, value): def __set__(self, instance, value):
""" """
Set the related objects through the reverse relation. Set the related objects through the reverse relation.
@ -520,9 +526,9 @@ class ReverseManyToOneDescriptor(object):
- ``value`` is the ``children`` sequence on the right of the equal sign - ``value`` is the ``children`` sequence on the right of the equal sign
""" """
warnings.warn( warnings.warn(
'Direct assignment to the reverse side of a related set is ' 'Direct assignment to the %s is deprecated due to the implicit '
'deprecated due to the implicit save() that happens. Use %s.set() ' 'save() that happens. Use %s.set() instead.' % self._get_set_deprecation_msg_params(),
'instead.' % self.rel.get_accessor_name(), RemovedInDjango20Warning, stacklevel=2, RemovedInDjango20Warning, stacklevel=2,
) )
manager = self.__get__(instance) manager = self.__get__(instance)
manager.set(value) manager.set(value)
@ -757,6 +763,12 @@ class ManyToManyDescriptor(ReverseManyToOneDescriptor):
reverse=self.reverse, reverse=self.reverse,
) )
def _get_set_deprecation_msg_params(self):
return ( # RemovedInDjango20Warning
'%s side of a many-to-many set' % ('reverse' if self.reverse else 'forward'),
self.rel.get_accessor_name() if self.reverse else self.field.name,
)
def create_forward_many_to_many_manager(superclass, rel, reverse): def create_forward_many_to_many_manager(superclass, rel, reverse):
""" """

View File

@ -400,15 +400,24 @@ class ManyToManyTests(TestCase):
self.a4.publications.set([], clear=True) self.a4.publications.set([], clear=True)
self.assertQuerysetEqual(self.a4.publications.all(), []) self.assertQuerysetEqual(self.a4.publications.all(), [])
def test_assign_deprecation(self): def test_assign_forward_deprecation(self):
msg = ( msg = (
"Direct assignment to the reverse side of a related set is " "Direct assignment to the reverse side of a many-to-many set is "
"deprecated due to the implicit save() that happens. Use " "deprecated due to the implicit save() that happens. Use "
"article_set.set() instead." "article_set.set() instead."
) )
with self.assertRaisesMessage(RemovedInDjango20Warning, msg): with self.assertRaisesMessage(RemovedInDjango20Warning, msg):
self.p2.article_set = [self.a4, self.a3] self.p2.article_set = [self.a4, self.a3]
def test_assign_reverse_deprecation(self):
msg = (
"Direct assignment to the forward side of a many-to-many "
"set is deprecated due to the implicit save() that happens. Use "
"publications.set() instead."
)
with self.assertRaisesMessage(RemovedInDjango20Warning, msg):
self.a1.publications = [self.p1, self.p2]
@ignore_warnings(category=RemovedInDjango20Warning) @ignore_warnings(category=RemovedInDjango20Warning)
def test_assign_deprecated(self): def test_assign_deprecated(self):
self.p2.article_set = [self.a4, self.a3] self.p2.article_set = [self.a4, self.a3]