Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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 jezdez authored
Showing with 19 additions and 1 deletion.
  1. +7 −1 django/db/models/base.py
  2. +12 −0 tests/modeltests/many_to_one/tests.py
8 django/db/models/base.py
View
@@ -20,7 +20,7 @@
from django.db.models import signals
from django.db.models.loading import register_models, get_model
from django.utils.translation import ugettext_lazy as _
-from django.utils.functional import curry
+from django.utils.functional import curry, Promise
from django.utils.encoding import smart_str, force_unicode
from django.utils.text import get_text_list, capfirst
@@ -297,10 +297,14 @@ def __init__(self, *args, **kwargs):
# is *not* consumed. We rely on this, so don't change the order
# without changing the logic.
for val, field in izip(args, fields_iter):
+ if isinstance(val, Promise):
+ val = force_unicode(val)
setattr(self, field.attname, val)
else:
# Slower, kwargs-ready version.
for val, field in izip(args, fields_iter):
+ if isinstance(val, Promise):
+ val = force_unicode(val)
setattr(self, field.attname, val)
kwargs.pop(field.name, None)
# Maintain compatibility with existing calls.
@@ -354,6 +358,8 @@ def __init__(self, *args, **kwargs):
# checked) by the RelatedObjectDescriptor.
setattr(self, field.name, rel_obj)
else:
+ if isinstance(val, Promise):
+ val = force_unicode(val)
setattr(self, field.attname, val)
if kwargs:
12 tests/modeltests/many_to_one/tests.py
View
@@ -5,6 +5,7 @@
from django.core.exceptions import MultipleObjectsReturned
from django.test import TestCase
+from django.utils.translation import ugettext_lazy
from .models import Article, Reporter
@@ -412,3 +413,14 @@ def test_manager_class_caching(self):
# Same as each other
self.assertTrue(r1.article_set.__class__ is r2.article_set.__class__)
+
+ def test_create_relation_with_ugettext_lazy(self):
+ reporter = Reporter.objects.create(first_name='John',
+ last_name='Smith',
+ email='john.smith@example.com')
+ lazy = ugettext_lazy(u'test')
+ reporter.article_set.create(headline=lazy,
+ pub_date=datetime(2011, 6, 10))
+ notlazy = unicode(lazy)
+ article = reporter.article_set.get()
+ self.assertEqual(article.headline, notlazy)
Please sign in to comment.
Something went wrong with that request. Please try again.