Repository navigation
Closed
Labels
Milestone
Description
This from IRC...
[17:04:05] <__zer01> after adding the `min_value` kwarg to a serializer.DecimalField I get a `django.core.exceptions.AppRegistryNotReady: The translation infrastructure cannot be initialized before the apps registry is ready.` error when running tests, any ideas?
[17:07:57] <__zer01> here's the end of the stack trace http://pastebin.com/B7ECdL6w
Which is:
File "/home/eugenio/spartan/rest/serializers.py", line 80, in MySerializer min_value=1.01)
File "/home/eugenio/Envs/spartan/lib/python3.4/site-packages/rest_framework/fields.py", line 906, in __init__
message = self.error_messages['min_value'].format(min_value=self.min_value)
File "/home/eugenio/Envs/spartan/lib/python3.4/site-packages/django/utils/functional.py", line 136, in __wrapper__ res = func(*self.__args, **self.__kw)
File "/home/eugenio/Envs/spartan/lib/python3.4/site-packages/django/utils/translation/__init__.py", line 84, in ugettext
return _trans.ugettext(message)
File "/home/eugenio/Envs/spartan/lib/python3.4/site-packages/django/utils/translation/trans_real.py", line 321, in gettext
return do_translate(message, 'gettext')
File "/home/eugenio/Envs/spartan/lib/python3.4/site-packages/django/utils/translation/trans_real.py", line 304, in do_translate
_default = _default or translation(settings.LANGUAGE_CODE)
File "/home/eugenio/Envs/spartan/lib/python3.4/site-packages/django/utils/translation/trans_real.py", line 206, in translation
_translations[language] = DjangoTranslation(language)
File "/home/eugenio/Envs/spartan/lib/python3.4/site-packages/django/utils/translation/trans_real.py", line 116, in __init__
self._add_installed_apps_translations()
File "/home/eugenio/Envs/spartan/lib/python3.4/site-packages/django/utils/translation/trans_real.py", line 164, in _add_installed_apps_translations
"The translation infrastructure cannot be initialized before the "
django.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.
And...
[17:08:41] yuriheupa ([email protected]) joined the channel.
[17:09:26] yuriheupa ([email protected]) left IRC. (Remote host closed the connection)
[17:11:54] <tomchristie> Hrm
[17:13:14] <__zer01> I'm running djangorestframework==3.2.3 and Django==1.8.4
[17:13:28] <tomchristie> I think that may need raising as an issue
[17:14:13] <tomchristie> The error is being raised because we're calling .format on a lazy-translated error message
[17:14:34] <tomchristie> Due to it being passed a min_value which is included in the message
[17:15:00] <tomchristie> Prob is, that causes the translation machinary to attempt to run, and produce the translated output
[17:15:12] <tomchristie> But that can't happen yet because the app isn't setup and running
[17:15:23] <tomchristie> Looks like Django internally handles this a little diff, eg
[17:15:34] <tomchristie> This… https://github.com/django/django/blob/master/django/forms/fields.py#L258
[17:15:43] <tomchristie> vs
[17:16:09] <tomchristie> This: https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/fields.py#L820-L821
[17:16:23] <tomchristie> Notive that we're calling .format when the field is instantiated
[17:16:31] <tomchristie> But same is not happening in Django
[17:16:37] <tomchristie> You may be able to resolve
[17:16:53] <tomchristie> by ensuring that you're not importing the serializers prior to the app setup
[17:16:54] marc_v92 (~marc_v92@unaffiliated/marc-v92/x-6202358) joined the channel.
[17:17:07] <tomchristie> (Eg if you import them in your settings I guess that's when it blows up)
Reacted by Taras Yanchynskyy and Alex Subbotin
Activity
nryoung commentedon Sep 11, 2015
Really all that needs to be changed here is that these particular lines
need to be removed?
Doing this and running the tests does not yield any test failures, but then again I am not sure where else
messageis used.nryoung commentedon Sep 15, 2015
ping @tomchristie or @kevin-brown for the above. If this is the right approach and I have already made the changes and could push them out. Let me know when you get a chance 😄
xordoquy commentedon Sep 15, 2015
@nryoung we may lack a test for that particular string. It looks like the omitting the format will result in the max_value not being correctly filled whenever an error occurs.
nryoung commentedon Sep 15, 2015
This does seem to be the case. It seems like we could use the
messageclass attribute of Django'sMaxValueValidatorsince it is a simple inheritance of this class: https://github.com/django/django/blob/master/django/core/validators.py#L318nryoung commentedon Sep 16, 2015
I have been doing some research on this.
For Django versions >= 1.8
messageis optional in the__init__method:but,
messageis also defaulted as a class attribute here:For Django versions < 1.8
messageis not expected:thus, that is why it is popped off here:
but,
messageis also defaulted here:I believe, that removing the
messageassignments here and other similar ones:and not passing
messagein to the instantiation of the validator, it should default to the Django class attribute:I will make a PR for what I outline above. As always, let me know if I am wrong here.
Do not format error message on validators, fixes encode#3354
Don't format error messages on validators, fixes encode#3354
Remove formatted messages as parameters to field bound validators, fixes
Remove formatted messages as parameters to field bound validators, fixes
86 remaining items
Avoid Django's lazy() when creating validators because it is too slow
Avoid Django's lazy() when creating validators because it is too slow
Avoid Django's lazy() when creating validators because it is too slow
Avoid Django's lazy() when creating validators because it is too slow