From 04e7c7d51f832407c49c3abb3413fbfbdb410f6f Mon Sep 17 00:00:00 2001 From: Baptiste Mispelon Date: Tue, 16 Sep 2014 12:17:33 +0200 Subject: [PATCH] [1.7.x] Fixed #23492 -- Restored F.__deepcopy__. This reverts commit 3a66035107a640c4905f0a5f247a45f32dbe16d7. A regression test was also added. Backport of d63ac5b595694c44ed8a64d782a177d2f92125d9 from master. --- django/db/models/expressions.py | 5 +++++ tests/expressions/tests.py | 10 ++++++++++ 2 files changed, 15 insertions(+) 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)