Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #10498 -- Fixed using ugettext_lazy values when creating model …

…instances. Thanks to Claude Paroz and Jonas Obrist.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17641 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit c988397279147b9d38c5b3feed9647c586fa049a 1 parent dc49e61
Jannis Leidel authored March 03, 2012
8  django/db/models/base.py
@@ -20,7 +20,7 @@
20 20
 from django.db.models import signals
21 21
 from django.db.models.loading import register_models, get_model
22 22
 from django.utils.translation import ugettext_lazy as _
23  
-from django.utils.functional import curry
  23
+from django.utils.functional import curry, Promise
24 24
 from django.utils.encoding import smart_str, force_unicode
25 25
 from django.utils.text import get_text_list, capfirst
26 26
 
@@ -297,10 +297,14 @@ def __init__(self, *args, **kwargs):
297 297
             # is *not* consumed. We rely on this, so don't change the order
298 298
             # without changing the logic.
299 299
             for val, field in izip(args, fields_iter):
  300
+                if isinstance(val, Promise):
  301
+                    val = force_unicode(val)
300 302
                 setattr(self, field.attname, val)
301 303
         else:
302 304
             # Slower, kwargs-ready version.
303 305
             for val, field in izip(args, fields_iter):
  306
+                if isinstance(val, Promise):
  307
+                    val = force_unicode(val)
304 308
                 setattr(self, field.attname, val)
305 309
                 kwargs.pop(field.name, None)
306 310
                 # Maintain compatibility with existing calls.
@@ -354,6 +358,8 @@ def __init__(self, *args, **kwargs):
354 358
                 # checked) by the RelatedObjectDescriptor.
355 359
                 setattr(self, field.name, rel_obj)
356 360
             else:
  361
+                if isinstance(val, Promise):
  362
+                    val = force_unicode(val)
357 363
                 setattr(self, field.attname, val)
358 364
 
359 365
         if kwargs:
12  tests/modeltests/many_to_one/tests.py
@@ -5,6 +5,7 @@
5 5
 
6 6
 from django.core.exceptions import MultipleObjectsReturned
7 7
 from django.test import TestCase
  8
+from django.utils.translation import ugettext_lazy
8 9
 
9 10
 from .models import Article, Reporter
10 11
 
@@ -412,3 +413,14 @@ def test_manager_class_caching(self):
412 413
 
413 414
         # Same as each other
414 415
         self.assertTrue(r1.article_set.__class__ is r2.article_set.__class__)
  416
+
  417
+    def test_create_relation_with_ugettext_lazy(self):
  418
+        reporter = Reporter.objects.create(first_name='John',
  419
+                                           last_name='Smith',
  420
+                                           email='john.smith@example.com')
  421
+        lazy = ugettext_lazy(u'test')
  422
+        reporter.article_set.create(headline=lazy,
  423
+                                    pub_date=datetime(2011, 6, 10))
  424
+        notlazy = unicode(lazy)
  425
+        article = reporter.article_set.get()
  426
+        self.assertEqual(article.headline, notlazy)

0 notes on commit c988397

Please sign in to comment.
Something went wrong with that request. Please try again.