Code

Ticket #10498: 10498-2.diff

File 10498-2.diff, 2.8 KB (added by claudep, 2 years ago)

Updated to latest trunk (r17503)

Line 
1diff --git a/django/db/models/base.py b/django/db/models/base.py
2index ebd67be..edd7758 100644
3--- a/django/db/models/base.py
4+++ b/django/db/models/base.py
5@@ -20,7 +20,7 @@ from django.db.models.options import Options
6 from django.db.models import signals
7 from django.db.models.loading import register_models, get_model
8 from django.utils.translation import ugettext_lazy as _
9-from django.utils.functional import curry
10+from django.utils.functional import curry, Promise
11 from django.utils.encoding import smart_str, force_unicode
12 from django.utils.text import get_text_list, capfirst
13 
14@@ -298,10 +298,14 @@ class Model(object):
15             # is *not* consumed. We rely on this, so don't change the order
16             # without changing the logic.
17             for val, field in izip(args, fields_iter):
18+                if isinstance(val, Promise):
19+                    val = force_unicode(val)
20                 setattr(self, field.attname, val)
21         else:
22             # Slower, kwargs-ready version.
23             for val, field in izip(args, fields_iter):
24+                if isinstance(val, Promise):
25+                    val = force_unicode(val)
26                 setattr(self, field.attname, val)
27                 kwargs.pop(field.name, None)
28                 # Maintain compatibility with existing calls.
29@@ -355,6 +359,8 @@ class Model(object):
30                 # checked) by the RelatedObjectDescriptor.
31                 setattr(self, field.name, rel_obj)
32             else:
33+                if isinstance(val, Promise):
34+                    val = force_unicode(val)
35                 setattr(self, field.attname, val)
36 
37         if kwargs:
38diff --git a/tests/modeltests/many_to_one/tests.py b/tests/modeltests/many_to_one/tests.py
39index bc9fe64..d9d67bb 100644
40--- a/tests/modeltests/many_to_one/tests.py
41+++ b/tests/modeltests/many_to_one/tests.py
42@@ -5,6 +5,7 @@ from datetime import datetime
43 
44 from django.core.exceptions import MultipleObjectsReturned
45 from django.test import TestCase
46+from django.utils.translation import ugettext_lazy
47 
48 from .models import Article, Reporter
49 
50@@ -412,3 +413,14 @@ class ManyToOneTests(TestCase):
51 
52         # Same as each other
53         self.assertTrue(r1.article_set.__class__ is r2.article_set.__class__)
54+
55+    def test_create_relation_with_ugettext_lazy(self):
56+        reporter = Reporter.objects.create(first_name='John',
57+                                           last_name='Smith',
58+                                           email='john.smith@example.com')
59+        lazy = ugettext_lazy(u'test')
60+        reporter.article_set.create(headline=lazy,
61+                                    pub_date=datetime(2011, 6, 10))
62+        notlazy = unicode(lazy)
63+        article = reporter.article_set.get()
64+        self.assertEqual(article.headline, notlazy)