Opened 9 years ago

Closed 9 years ago

Last modified 7 years ago

#8011 closed (fixed)

Translated strings in a ForeignKey of a model in an application not in the project root causes errors

Reported by: anonymous Owned by: Malcolm Tredinnick
Component: Translations Version: master
Severity: Keywords: translation, lazy, model
Cc: oliver@… Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description (last modified by Malcolm Tredinnick)

This seems like a real edge-case, but still it is an issue (for me at least). Since the memory optimization stuff from [8127], if I have a lazily translated string (in this case as the verbose_name) for a ForeignKey, which is in a model located in an application not in the project root (in this case in project.generic.tagging, I get this lovely error:

Traceback (most recent call last):
  File "", line 11, in <module>
  File "/usr/local/obeattie/", line 301, in execute_manager
  File "/usr/local/obeattie/", line 248, in execute
  File "/sw/lib/python2.5/site-packages/django/core/management/", line 77, in run_from_argv
  File "/sw/lib/python2.5/site-packages/django/core/management/", line 86, in execute
  File "/usr/local/django/trunk/django/utils/translation/", line 73, in activate
    return real_activate(language)
  File "/usr/local/django/trunk/django/utils/translation/", line 43, in delayed_loader
    return g['real_%s' % caller](*args, **kwargs)
  File "/Library/Python/2.5/site-packages/django/utils/translation/", line 209, in activate
    _active[currentThread()] = translation(language)
  File "/Library/Python/2.5/site-packages/django/utils/translation/", line 198, in translation
    default_translation = _fetch(settings.LANGUAGE_CODE)
  File "/Library/Python/2.5/site-packages/django/utils/translation/", line 181, in _fetch
    app = getattr(__import__(appname[:p], {}, {}, [appname[p+1:]]), appname[p+1:])
  File "/usr/local/obeattie/../obeattie/generic/tagging/", line 1, in <module>
    import obeattie.generic.tagging.admin
  File "/usr/local/obeattie/../obeattie/generic/tagging/", line 3, in <module>
    from obeattie.generic.tagging import models as tagging_models
  File "/usr/local/obeattie/../obeattie/generic/tagging/", line 500, in <module>
    class TaggedItem(models.Model):
  File "/Library/Python/2.5/site-packages/django/db/models/", line 89, in __new__
    new_class.add_to_class(obj_name, obj)
  File "/Library/Python/2.5/site-packages/django/db/models/", line 140, in add_to_class
    value.contribute_to_class(cls, name)
  File "/Library/Python/2.5/site-packages/django/db/models/fields/", line 672, in contribute_to_class
    super(ForeignKey, self).contribute_to_class(cls, name)
  File "/Library/Python/2.5/site-packages/django/db/models/fields/", line 113, in contribute_to_class
    self.do_related_class(other, cls)
  File "/Library/Python/2.5/site-packages/django/db/models/fields/", line 121, in do_related_class
  File "/Library/Python/2.5/site-packages/django/db/models/fields/", line 117, in set_attributes_from_rel
    self.verbose_name = self.verbose_name or
  File "/Library/Python/2.5/site-packages/django/utils/", line 188, in __wrapper__
    res = self.__func(*self.__args, **self.__kw)
  File "/usr/local/django/trunk/django/utils/translation/", line 62, in ugettext
    return real_ugettext(message)
  File "/Library/Python/2.5/site-packages/django/utils/translation/", line 288, in ugettext
    return do_translate(message, 'ugettext')
  File "/Library/Python/2.5/site-packages/django/utils/translation/", line 278, in do_translate
    _default = translation(settings.LANGUAGE_CODE)
  File "/Library/Python/2.5/site-packages/django/utils/translation/", line 198, in translation
    default_translation = _fetch(settings.LANGUAGE_CODE)
  File "/Library/Python/2.5/site-packages/django/utils/translation/", line 181, in _fetch
    app = getattr(__import__(appname[:p], {}, {}, [appname[p+1:]]), appname[p+1:])
AttributeError: 'module' object has no attribute 'tagging'

(This actually happened in django-tagging). I've tracked it down to changes made to or in django.utils.functional.lazy.__proxy__.__promise__.__wrapper__ — it doesn't appear that it got called prior to these changes (in [8119] at least), if that helps at all.

Attachments (1)

8011.diff (770 bytes) - added by datakid 7 years ago.
A cleaner patch with same results

Download all attachments as: .zip

Change History (5)

comment:1 Changed 9 years ago by oliver@…

Cc: oliver@… added

Damn, I reported this as anonymous. Adding myself to the Cc.

comment:2 Changed 9 years ago by Malcolm Tredinnick

Description: modified (diff)
Owner: changed from nobody to Malcolm Tredinnick
Triage Stage: UnreviewedAccepted

Thanks for the report. There are no edge cases in this are -- they're all real bugs.

The "problem" is that we're now copying all of the methods from the underlying function return value to the lazy object, so even things like bool() (e.g. if self.verbose_name:...) end up triggering a translation and that's a bad thing to do whilst the apps are loading. I'll hook up a quick hack to fix this and investigate a longer term solution when I get a chance.

comment:3 Changed 9 years ago by Malcolm Tredinnick

Resolution: fixed
Status: newclosed

(In [8132]) Fixed #8011 -- Be careful not to evalute lazy-translated Meta.verbose_name
values whilst importing models.

Changed 7 years ago by datakid

Attachment: 8011.diff added

A cleaner patch with same results

comment:4 Changed 7 years ago by datakid

Given information discovered in #5373, I have attached a patch that gives the same result, refactored.

Note: See TracTickets for help on using tickets.
Back to Top