Code

Opened 6 years ago

Closed 6 years ago

Last modified 4 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: mtredinnick
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 mtredinnick)

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 "manage.py", line 11, in <module>
    execute_manager(settings)
  File "/usr/local/obeattie/__init__.py", line 301, in execute_manager
    
  File "/usr/local/obeattie/__init__.py", line 248, in execute
    
  File "/sw/lib/python2.5/site-packages/django/core/management/base.py", line 77, in run_from_argv
  File "/sw/lib/python2.5/site-packages/django/core/management/base.py", line 86, in execute
  File "/usr/local/django/trunk/django/utils/translation/__init__.py", line 73, in activate
    return real_activate(language)
  File "/usr/local/django/trunk/django/utils/translation/__init__.py", line 43, in delayed_loader
    return g['real_%s' % caller](*args, **kwargs)
  File "/Library/Python/2.5/site-packages/django/utils/translation/trans_real.py", line 209, in activate
    _active[currentThread()] = translation(language)
  File "/Library/Python/2.5/site-packages/django/utils/translation/trans_real.py", line 198, in translation
    default_translation = _fetch(settings.LANGUAGE_CODE)
  File "/Library/Python/2.5/site-packages/django/utils/translation/trans_real.py", line 181, in _fetch
    app = getattr(__import__(appname[:p], {}, {}, [appname[p+1:]]), appname[p+1:])
  File "/usr/local/obeattie/../obeattie/generic/tagging/__init__.py", line 1, in <module>
    import obeattie.generic.tagging.admin
  File "/usr/local/obeattie/../obeattie/generic/tagging/admin.py", line 3, in <module>
    from obeattie.generic.tagging import models as tagging_models
  File "/usr/local/obeattie/../obeattie/generic/tagging/models.py", line 500, in <module>
    class TaggedItem(models.Model):
  File "/Library/Python/2.5/site-packages/django/db/models/base.py", line 89, in __new__
    new_class.add_to_class(obj_name, obj)
  File "/Library/Python/2.5/site-packages/django/db/models/base.py", line 140, in add_to_class
    value.contribute_to_class(cls, name)
  File "/Library/Python/2.5/site-packages/django/db/models/fields/related.py", 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/related.py", line 113, in contribute_to_class
    self.do_related_class(other, cls)
  File "/Library/Python/2.5/site-packages/django/db/models/fields/related.py", line 121, in do_related_class
    self.set_attributes_from_rel()
  File "/Library/Python/2.5/site-packages/django/db/models/fields/related.py", line 117, in set_attributes_from_rel
    self.verbose_name = self.verbose_name or self.rel.to._meta.verbose_name
  File "/Library/Python/2.5/site-packages/django/utils/functional.py", line 188, in __wrapper__
    res = self.__func(*self.__args, **self.__kw)
  File "/usr/local/django/trunk/django/utils/translation/__init__.py", line 62, in ugettext
    return real_ugettext(message)
  File "/Library/Python/2.5/site-packages/django/utils/translation/trans_real.py", line 288, in ugettext
    return do_translate(message, 'ugettext')
  File "/Library/Python/2.5/site-packages/django/utils/translation/trans_real.py", line 278, in do_translate
    _default = translation(settings.LANGUAGE_CODE)
  File "/Library/Python/2.5/site-packages/django/utils/translation/trans_real.py", line 198, in translation
    default_translation = _fetch(settings.LANGUAGE_CODE)
  File "/Library/Python/2.5/site-packages/django/utils/translation/trans_real.py", 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 4 years ago.
A cleaner patch with same results

Download all attachments as: .zip

Change History (5)

comment:1 Changed 6 years ago by oliver@…

  • Cc oliver@… added
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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

comment:2 Changed 6 years ago by mtredinnick

  • Description modified (diff)
  • Owner changed from nobody to mtredinnick
  • Triage Stage changed from Unreviewed to Accepted

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 6 years ago by mtredinnick

  • Resolution set to fixed
  • Status changed from new to closed

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

Changed 4 years ago by datakid

A cleaner patch with same results

comment:4 Changed 4 years ago by datakid

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

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.