diff --git a/django/db/models/expressions.py b/django/db/models/expressions.py index c23767d486..5484ca7f47 100644 --- a/django/db/models/expressions.py +++ b/django/db/models/expressions.py @@ -145,6 +145,11 @@ class F(ExpressionNode): super(F, self).__init__(None, None, False) self.name = name + def __deepcopy__(self, memodict): + obj = super(F, self).__deepcopy__(memodict) + obj.name = self.name + return obj + def prepare(self, evaluator, query, allow_joins): return evaluator.prepare_leaf(self, query, allow_joins) diff --git a/tests/expressions/tests.py b/tests/expressions/tests.py index 320271b1dc..0430fd0358 100644 --- a/tests/expressions/tests.py +++ b/tests/expressions/tests.py @@ -1,5 +1,7 @@ from __future__ import unicode_literals +from copy import deepcopy + from django.core.exceptions import FieldError from django.db.models import F from django.db import transaction @@ -281,3 +283,11 @@ class ExpressionsTests(TestCase): company_ceo_set__num_employees=F('company_ceo_set__num_employees') ) self.assertEqual(str(qs.query).count('JOIN'), 2) + + def test_F_object_deepcopy(self): + """ + Make sure F objects can be deepcopied (#23492) + """ + f = F("foo") + g = deepcopy(f) + self.assertEqual(f.name, g.name)