{"action":"created","issue":{"url":"https://api.github.com/repos/tomchristie/django-rest-framework/issues/2603","labels_url":"https://api.github.com/repos/tomchristie/django-rest-framework/issues/2603/labels{/name}","comments_url":"https://api.github.com/repos/tomchristie/django-rest-framework/issues/2603/comments","events_url":"https://api.github.com/repos/tomchristie/django-rest-framework/issues/2603/events","html_url":"https://github.com/tomchristie/django-rest-framework/issues/2603","id":58939255,"number":2603,"title":"Field error messages translation in `Field.__init__` evaluates lazy translation objects before apps are loaded","user":{"login":"fabianbuechler","id":679720,"avatar_url":"https://avatars.githubusercontent.com/u/679720?v=3","gravatar_id":"","url":"https://api.github.com/users/fabianbuechler","html_url":"https://github.com/fabianbuechler","followers_url":"https://api.github.com/users/fabianbuechler/followers","following_url":"https://api.github.com/users/fabianbuechler/following{/other_user}","gists_url":"https://api.github.com/users/fabianbuechler/gists{/gist_id}","starred_url":"https://api.github.com/users/fabianbuechler/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fabianbuechler/subscriptions","organizations_url":"https://api.github.com/users/fabianbuechler/orgs","repos_url":"https://api.github.com/users/fabianbuechler/repos","events_url":"https://api.github.com/users/fabianbuechler/events{/privacy}","received_events_url":"https://api.github.com/users/fabianbuechler/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-02-25T17:30:22Z","updated_at":"2015-02-25T20:29:25Z","closed_at":"2015-02-25T20:29:25Z","body":"Hi, I've noticed that when using Sphinx's autodoc to document serializers, the serializer fields cause errors due to lazy gettext objects being evaluated on `Field.__init__` in `CharField`, `IntegerField`, `FloatField` and `DecimalField` when `str.format()`, or in that case `lazy.__proxy__.format()` is being called:\r\n```python\r\nmessage = self.error_messages['max_length'].format(max_length=max_length)\r\n```\r\n\r\nThis works at runtime, but Sphinx's autodoc does not seem to load the apps infrastructure, thus calls to non-lazy gettext objects are forbidden, and of course also converting lazy gettext objects to strings.\r\n\r\nIs there any way to not do this on `Field.__init__` or to use partials for example like:\r\n\r\n```python\r\nfrom functools import partial\r\n\r\nmessage = partial(self.error_messages['max_length'].format, max_length=max_length)\r\n```\r\n\r\nand then evaluate this when the error message is required by checking for callables or so?\r\n\r\n\r\nThe full stracktrace looks like this:\r\n\r\n```python\r\n###/docs/technical/api_serializers.rst:16: WARNING: autodoc: failed to import module 'buchklub.api.serializers.product'; the following exception was raised:\r\nTraceback (most recent call last):\r\n  File \"###/.virtualenvs/bk/lib/python3.4/site-packages/django/utils/translation/trans_real.py\", line 186, in _fetch\r\n    app_configs = reversed(list(apps.get_app_configs()))\r\n  File \"###/.virtualenvs/bk/lib/python3.4/site-packages/django/apps/registry.py\", line 137, in get_app_configs\r\n    self.check_apps_ready()\r\n  File \"###/.virtualenvs/bk/lib/python3.4/site-packages/django/apps/registry.py\", line 124, in check_apps_ready\r\n    raise AppRegistryNotReady(\"Apps aren't loaded yet.\")\r\ndjango.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.\r\n\r\nDuring handling of the above exception, another exception occurred:\r\n\r\nTraceback (most recent call last):\r\n  File \"###/.virtualenvs/bk/lib/python3.4/site-packages/sphinx/ext/autodoc.py\", line 335, in import_object\r\n    __import__(self.modname)\r\n  File \"###/buchklub/api/serializers/product.py\", line 25, in <module>\r\n    class ProductSyncSerializer(CustomModelUpdateSerializer):\r\n  File \"###/buchklub/api/serializers/product.py\", line 37, in ProductSyncSerializer\r\n    min_length=10, max_length=10, allow_blank=False)\r\n  File \"###/.virtualenvs/bk/lib/python3.4/site-packages/rest_framework/fields.py\", line 562, in __init__\r\n    message = self.error_messages['max_length'].format(max_length=max_length)\r\n  File \"###/.virtualenvs/bk/lib/python3.4/site-packages/django/utils/functional.py\", line 132, in __wrapper__\r\n    res = func(*self.__args, **self.__kw)\r\n  File \"###/.virtualenvs/bk/lib/python3.4/site-packages/django/utils/translation/__init__.py\", line 83, in ugettext\r\n    return _trans.ugettext(message)\r\n  File \"###/.virtualenvs/bk/lib/python3.4/site-packages/django/utils/translation/trans_real.py\", line 319, in gettext\r\n    return do_translate(message, 'gettext')\r\n  File \"###/.virtualenvs/bk/lib/python3.4/site-packages/django/utils/translation/trans_real.py\", line 306, in do_translate\r\n    _default = translation(settings.LANGUAGE_CODE)\r\n  File \"###/.virtualenvs/bk/lib/python3.4/site-packages/django/utils/translation/trans_real.py\", line 209, in translation\r\n    default_translation = _fetch(settings.LANGUAGE_CODE)\r\n  File \"###/.virtualenvs/bk/lib/python3.4/site-packages/django/utils/translation/trans_real.py\", line 189, in _fetch\r\n    \"The translation infrastructure cannot be initialized before the \"\r\ndjango.core.exceptions.AppRegistryNotReady: The translation infrastructure cannot be initialized before the apps registry is ready. Check that you don't make non-lazy gettext calls at import time.\r\n###/docs/technical/api_serializers.rst:22: WARNING: autodoc: failed to import module 'buchklub.api.serializers.order'; the following exception was raised:\r\nTraceback (most recent call last):\r\n  File \"###/.virtualenvs/bk/lib/python3.4/site-packages/django/utils/translation/trans_real.py\", line 186, in _fetch\r\n    app_configs = reversed(list(apps.get_app_configs()))\r\n  File \"###/.virtualenvs/bk/lib/python3.4/site-packages/django/apps/registry.py\", line 137, in get_app_configs\r\n    self.check_apps_ready()\r\n  File \"###/.virtualenvs/bk/lib/python3.4/site-packages/django/apps/registry.py\", line 124, in check_apps_ready\r\n    raise AppRegistryNotReady(\"Apps aren't loaded yet.\")\r\ndjango.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.\r\n\r\nDuring handling of the above exception, another exception occurred:\r\n\r\nTraceback (most recent call last):\r\n  File \"###/.virtualenvs/bk/lib/python3.4/site-packages/sphinx/ext/autodoc.py\", line 335, in import_object\r\n    __import__(self.modname)\r\n  File \"###/buchklub/api/serializers/order.py\", line 13, in <module>\r\n    class OrderItemSyncSerializer(serializers.Serializer):\r\n  File \"###/buchklub/api/serializers/order.py\", line 32, in OrderItemSyncSerializer\r\n    max_digits=10, decimal_places=2, min_value=0, allow_null=True)\r\n  File \"###/.virtualenvs/bk/lib/python3.4/site-packages/rest_framework/fields.py\", line 745, in __init__\r\n    message = self.error_messages['min_value'].format(min_value=min_value)\r\n  File \"###/.virtualenvs/bk/lib/python3.4/site-packages/django/utils/functional.py\", line 132, in __wrapper__\r\n    res = func(*self.__args, **self.__kw)\r\n  File \"###/.virtualenvs/bk/lib/python3.4/site-packages/django/utils/translation/__init__.py\", line 83, in ugettext\r\n    return _trans.ugettext(message)\r\n  File \"###/.virtualenvs/bk/lib/python3.4/site-packages/django/utils/translation/trans_real.py\", line 319, in gettext\r\n    return do_translate(message, 'gettext')\r\n  File \"###/.virtualenvs/bk/lib/python3.4/site-packages/django/utils/translation/trans_real.py\", line 306, in do_translate\r\n    _default = translation(settings.LANGUAGE_CODE)\r\n  File \"###/.virtualenvs/bk/lib/python3.4/site-packages/django/utils/translation/trans_real.py\", line 209, in translation\r\n    default_translation = _fetch(settings.LANGUAGE_CODE)\r\n  File \"###/.virtualenvs/bk/lib/python3.4/site-packages/django/utils/translation/trans_real.py\", line 189, in _fetch\r\n    \"The translation infrastructure cannot be initialized before the \"\r\ndjango.core.exceptions.AppRegistryNotReady: The translation infrastructure cannot be initialized before the apps registry is ready. Check that you don't make non-lazy gettext calls at import time.\r\n```"},"comment":{"url":"https://api.github.com/repos/tomchristie/django-rest-framework/issues/comments/76049294","html_url":"https://github.com/tomchristie/django-rest-framework/issues/2603#issuecomment-76049294","issue_url":"https://api.github.com/repos/tomchristie/django-rest-framework/issues/2603","id":76049294,"user":{"login":"tomchristie","id":647359,"avatar_url":"https://avatars.githubusercontent.com/u/647359?v=3","gravatar_id":"","url":"https://api.github.com/users/tomchristie","html_url":"https://github.com/tomchristie","followers_url":"https://api.github.com/users/tomchristie/followers","following_url":"https://api.github.com/users/tomchristie/following{/other_user}","gists_url":"https://api.github.com/users/tomchristie/gists{/gist_id}","starred_url":"https://api.github.com/users/tomchristie/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tomchristie/subscriptions","organizations_url":"https://api.github.com/users/tomchristie/orgs","repos_url":"https://api.github.com/users/tomchristie/repos","events_url":"https://api.github.com/users/tomchristie/events{/privacy}","received_events_url":"https://api.github.com/users/tomchristie/received_events","type":"User","site_admin":false},"created_at":"2015-02-25T20:29:25Z","updated_at":"2015-02-25T20:29:25Z","body":"I don't know, but this sounds like a third-party error to me - probably need to be calling `django.setup()` before attempting to do anything that involves the django settings. I could reconsider if there's a good way of testing this, ensure we don't regress, and a simple route to demonstrating that we can do something that allows this to work without any wide-ranging coded changes. (In short if someone wants to champion this and does the legwork, perhaps we can reconsider it as an open issue, otherwise look at ensuring `django.setup()` gets called first)\r\n"}}