Ticket #10498: 10498.diff

File 10498.diff, 2.9 KB (added by Jonas Obrist, 13 years ago)

a fix as discussed with russ

  • django/db/models/base.py

    diff --git a/django/db/models/base.py b/django/db/models/base.py
    index 31310ea..f1632f2 100644
    a b from django.db import (connections, router, transaction, DatabaseError,  
    1919from django.db.models import signals
    2020from django.db.models.loading import register_models, get_model
    2121from django.utils.translation import ugettext_lazy as _
    22 from django.utils.functional import curry
     22from django.utils.functional import curry, Promise
    2323from django.utils.encoding import smart_str, force_unicode
    2424from django.utils.text import get_text_list, capfirst
    2525from django.conf import settings
    class Model(object):  
    297297            # is *not* consumed. We rely on this, so don't change the order
    298298            # without changing the logic.
    299299            for val, field in izip(args, fields_iter):
     300                if isinstance(val, Promise):
     301                    val = force_unicode(val)
    300302                setattr(self, field.attname, val)
    301303        else:
    302304            # Slower, kwargs-ready version.
    303305            for val, field in izip(args, fields_iter):
     306                if isinstance(val, Promise):
     307                    val = force_unicode(val)
    304308                setattr(self, field.attname, val)
    305309                kwargs.pop(field.name, None)
    306310                # Maintain compatibility with existing calls.
    class Model(object):  
    354358                # checked) by the RelatedObjectDescriptor.
    355359                setattr(self, field.name, rel_obj)
    356360            else:
     361                if isinstance(val, Promise):
     362                    val = force_unicode(val)
    357363                setattr(self, field.attname, val)
    358364
    359365        if kwargs:
  • tests/modeltests/many_to_one/tests.py

    diff --git a/tests/modeltests/many_to_one/tests.py b/tests/modeltests/many_to_one/tests.py
    index 8a2af21..cb31c75 100644
    a b from datetime import datetime  
    33
    44from django.test import TestCase
    55from django.core.exceptions import FieldError
     6from django.utils.translation import ugettext_lazy
    67
    78from models import Article, Reporter
    89
    class ManyToOneTests(TestCase):  
    372373        # recursive don't cause recursion depth problems under deepcopy.
    373374        self.r.cached_query = Article.objects.filter(reporter=self.r)
    374375        self.assertEqual(repr(deepcopy(self.r)), "<Reporter: John Smith>")
     376
     377    def test_create_relation_with_ugettext_lazy(self):
     378        reporter = Reporter.objects.create(first_name='John',
     379                                           last_name='Smith',
     380                                           email='john.smith@example.com')
     381        lazy = ugettext_lazy(u'test')
     382        reporter.article_set.create(headline=lazy,
     383                                    pub_date=datetime(2011, 6, 10))
     384        notlazy = unicode(lazy)
     385        article = reporter.article_set.get()
     386        self.assertEqual(article.headline, notlazy)
Back to Top