-
Notifications
You must be signed in to change notification settings - Fork 151
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
Django 1.8 get_language returns None #90
Comments
Yikes! That clearly has some implications. I wonder whether we should take care of this though - after all because Django wants to be explicit about setting a language first before doing I18N calls. I think bailing out with an error would be better then trying do insert Note you can use the following to create objects on the shell:
(I've seen people writing such code expecting it to work, so this API was added). |
Hello, This is causing a crash for us when using 58 return "({}) {}".format(
59 self.pk,
---> 60 self.safe_translation_getter('name', any_language=True),
61 )
62
/home/alopez/.virtualenvs/project/lib/python3.4/site-packages/parler/models.py in safe_translation_getter(self, field, default, language_code, any_language)
702 # which also attempts the fallback language if configured to do so.
703 try:
--> 704 return getattr(self, field)
705 except TranslationDoesNotExist:
706 pass
/home/alopez/.virtualenvs/project/lib/python3.4/site-packages/parler/fields.py in __get__(self, instance, instance_type)
88 meta = self.field.meta
89 try:
---> 90 translation = instance._get_translated_model(use_fallback=True, meta=meta)
91 except meta.model.DoesNotExist as e:
92 if self.field.any_language:
/home/alopez/.virtualenvs/project/lib/python3.4/site-packages/parler/models.py in _get_translated_model(self, language_code, use_fallback, auto_create, meta)
470 # 4. Fallback?
471 fallback_msg = None
--> 472 lang_dict = get_language_settings(language_code)
473
474 if language_code not in local_cache:
/home/alopez/.virtualenvs/project/lib/python3.4/site-packages/parler/utils/i18n.py in get_language_settings(language_code, site_id)
72 # to have their own variation of the settings with this method functionality included.
73 from parler import appsettings
---> 74 return appsettings.PARLER_LANGUAGES.get_language(language_code, site_id)
75
76
/home/alopez/.virtualenvs/project/lib/python3.4/site-packages/parler/utils/conf.py in get_language(self, language_code, site_id)
112 # no language match, search for variant: fr-ca falls back to fr
113 for lang_dict in self.get(site_id, ()):
--> 114 if lang_dict['code'].split('-')[0] == language_code.split('-')[0]:
115 return lang_dict
116
AttributeError: 'NoneType' object has no attribute 'split' The instance in question has a single translation but translations are not active (in the shell in this case), the field does not have The error happens in the call to |
I would also like to add that I'm doing extensive use of parler from outside the request-response cycle (i.e. for automatic translations in background -- celery), so I'd have to hack parler quite hard if I suggest we write a wrapper like people at modeltranslations has done: https://github.com/deschler/django-modeltranslation/blob/master/modeltranslation/utils.py#L13 . This way it could even be configurable with a new settings like |
For cases where parler is used outside of request-response cycle (management commands, celery tasks) did you guys @AdrianLC, @CptLemming are activating translation system with If you are not activating a translation then this is intended behavior from Django 1.8, and you need to activate some language (and probably you need to correct your code to activate the right language based on other criteria). If this is not the case then probably something in django-parler need to be fixed. |
For the next release, I've added a warning in 8bcc2ae + 0bffe14 that reminds you to use |
@vdboor Now when I'm looking more deeply into this I think that one thing can be changed in I think that this is the reason for
Which is kind of overhead for every model's If you use What you think about that? |
With respect to I wonder, you mentioned that you need to use I still fear we open a can of worms if we support something in this direction. All other holes also need to be closed then. |
I guess that supporting this transparently it's a bit of a nightmare, and probably it will mean going too far |
@vdboor the reason that I use It's kind of annoying every time when I open the shell and to import @vdboor @yakky |
Now when I'm playing with django's translation system I can't see a situation when error will be raised when translations are not activated. It just returns the hardcoded text in code without checking any The reason for that I think is mostly because of migration framework to not create migrations all the time based on translations in In As I said in my previous comment I understand that too many magic is bad thing. But now I think that if we came up with well defined behavior which is reasonable will be better. For initial issue reported by @CptLemming there is nothing that can be done in For what @AdrianLC reported as a comment in the issue I think that is more legit to be changed something in |
I don't think parler should crash like this, sending a null to the database, even when wrongly creating an instance without activating the translation machinery. IMO there should be some kind of fail-safe assertion within parler that would report the cause of error, without actually sending the query. As for the if not get_language():
with translations.override(settings.LANGUAGE_CODE):
return self.safe_translation_getter('field', any_language=True)
return self.safe_translation_getter('field', any_language=True) Your suggestion, @vdboor ( I understand the change of behavior in Django's part, as one might not need i18n and so the site can drop the weight of the localization middleware. But why would someone have parler installed, without wanting the translations activated, at all times (i.e. whenever calling something in parler). I still think that a wrapper would make more sense, even if it means that parler doesn't support deactivating translations at all. def get_language():
lang = _get_language() # original get_language
if lang is None: # Django >= 1.8
return settings.LANGUAGE_CODE
return lang |
Has this issue been resolved? I'm running into the same problem. |
For now, you're required to manually activate the translations:
we're a bit cautious to allow working with translations without having an language activated, it brings parler into untested territory. |
Stumbled upon this issue while using django-parler in our own code base. I did make this PR such that we can have the pre-Django 1.8 behavior back with a global setting optional setting. The default behavior would be to use Django's Would this be something that can be merged in? If not, what would be the alternative? |
Thanks for merging this in. Follow-up question: will there also be a (patch-level) version bump for this change (e.g. v1.6.6)? What is the typical release schedule / process you adhere to? Having a separate tag for this fix makes it easier for us to pull in the django-parler dependency. |
Django 1.8 changed the default behaviour of get_language:
Now, when fetching objects without specifying the language
language_code
is no longer populated:And I cannot create objects on the shell:
The text was updated successfully, but these errors were encountered: