diff --git a/django/db/models/base.py b/django/db/models/base.py index fc38224345..53b62df135 100644 --- a/django/db/models/base.py +++ b/django/db/models/base.py @@ -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: diff --git a/tests/modeltests/many_to_one/tests.py b/tests/modeltests/many_to_one/tests.py index bc9fe64be4..d9d67bbb98 100644 --- a/tests/modeltests/many_to_one/tests.py +++ b/tests/modeltests/many_to_one/tests.py @@ -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)