-
Notifications
You must be signed in to change notification settings - Fork 347
Calling "django.setup()" before the test collection #135
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hmm, the point of lazy translations is to defer the actual translation to a later point (typically when a view runs, when the language has been determined). Apart from peeking into app registry, the traceback shows accessing Therefore I think that this looks like a bug in Django form initialization. The translation (and therefore the app registry) should be delayed to form construction/rendering time. |
I understand (and kind of agree) to what you're saying, however... we're only trying to check if the translation is Actually, thinking of it, I can see two issues here: I'm going to close this one, and open a ticket on Django. Thanks for your analysis ;) |
For reference, the issue in Django's tracker is: https://code.djangoproject.com/ticket/23098 |
I am also getting same as fallows .. after upgrading django 1.7 -- How to resolve the issue. I already did changed wsgi file import os os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings.common") This application object is used by any WSGI server configured to use thisfile. This includes Django's development server, if the WSGI_APPLICATIONsetting points here.from django.core.wsgi import get_wsgi_application #=========================================================================== error on termial#============================================================================ |
I don't know the rationale for now calling
django.setup()
before the test collection (cf #119 (comment)), but what I know is that it's causing an issue in a corner case implying lazy translations in a modelForm field choice list, because of the new feature allowing one to customiz the empty choice.Here's how to reproduce:
1/ Create an app with the following
models.py
:2/ Now create a simple test file that imports this, eg
from foo.models import MyForm
3/ Here's the result:
This is a special case that only happens because we have translated named groups.
From my investigations, here's what happening:
When building the model form, it goes through the choice list to see if we've customized the empty choice: https://github.com/django/django/blob/master/django/db/models/fields/__init__.py#L732
To compare the choice with '' or None, we must first translate it for real (and thus "unlazy" the translation). Then, at the end, we try to merge all the translations together, and thus need to access the app configs (https://github.com/django/django/blob/master/django/utils/translation/trans_real.py#L163).
As we haven't run Django's setup yet (we're still in the test collection phase), the app-loading hasn't even started, and thus the failure.
I can obviously fix this issue by doing the setup myself in my conftest file, but I'm wondering if this should (could?) be fixed at pytest_django's level?
The text was updated successfully, but these errors were encountered: