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
This commit is contained in:
Jannis Leidel 2012-03-03 19:02:49 +00:00
parent dc49e6143a
commit c988397279
2 changed files with 19 additions and 1 deletions

View File

@ -20,7 +20,7 @@ from django.db.models.options import Options
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 @@ class Model(object):
# 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 @@ class Model(object):
# 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:

View File

@ -5,6 +5,7 @@ from datetime import datetime
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 @@ class ManyToOneTests(TestCase):
# 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)