diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py index 6737570074..5c1c8faf97 100644 --- a/django/db/models/fields/related.py +++ b/django/db/models/fields/related.py @@ -781,6 +781,7 @@ class ForeignObject(RelatedField): "ForeignObject.get_joining_columns() is deprecated. Use " "get_joining_fields() instead.", RemovedInDjango60Warning, + stacklevel=2, ) source = self.reverse_related_fields if reverse_join else self.related_fields return tuple( @@ -792,6 +793,7 @@ class ForeignObject(RelatedField): "ForeignObject.get_reverse_joining_columns() is deprecated. Use " "get_reverse_joining_fields() instead.", RemovedInDjango60Warning, + stacklevel=2, ) return self.get_joining_columns(reverse_join=True) diff --git a/django/db/models/fields/reverse_related.py b/django/db/models/fields/reverse_related.py index a39bfd07da..85378431a7 100644 --- a/django/db/models/fields/reverse_related.py +++ b/django/db/models/fields/reverse_related.py @@ -197,6 +197,7 @@ class ForeignObjectRel(FieldCacheMixin): "ForeignObjectRel.get_joining_columns() is deprecated. Use " "get_joining_fields() instead.", RemovedInDjango60Warning, + stacklevel=2, ) return self.field.get_reverse_joining_columns() diff --git a/tests/foreign_object/tests.py b/tests/foreign_object/tests.py index 2d3aa800f7..89ed85b658 100644 --- a/tests/foreign_object/tests.py +++ b/tests/foreign_object/tests.py @@ -703,24 +703,27 @@ class GetJoiningDeprecationTests(TestCase): "ForeignObject.get_joining_columns() is deprecated. Use " "get_joining_fields() instead." ) - with self.assertWarnsMessage(RemovedInDjango60Warning, msg): + with self.assertWarnsMessage(RemovedInDjango60Warning, msg) as ctx: Membership.person.field.get_joining_columns() + self.assertEqual(ctx.filename, __file__) def test_foreign_object_get_reverse_joining_columns_warning(self): msg = ( "ForeignObject.get_reverse_joining_columns() is deprecated. Use " "get_reverse_joining_fields() instead." ) - with self.assertWarnsMessage(RemovedInDjango60Warning, msg): + with self.assertWarnsMessage(RemovedInDjango60Warning, msg) as ctx: Membership.person.field.get_reverse_joining_columns() + self.assertEqual(ctx.filename, __file__) def test_foreign_object_rel_get_joining_columns_warning(self): msg = ( "ForeignObjectRel.get_joining_columns() is deprecated. Use " "get_joining_fields() instead." ) - with self.assertWarnsMessage(RemovedInDjango60Warning, msg): + with self.assertWarnsMessage(RemovedInDjango60Warning, msg) as ctx: Membership.person.field.remote_field.get_joining_columns() + self.assertEqual(ctx.filename, __file__) def test_join_get_joining_columns_warning(self): class CustomForeignKey(models.ForeignKey):