From 98e4aba62798ff6be5ffbbe4b597bb03c9b5814a Mon Sep 17 00:00:00 2001 From: andrey-skvortsov Date: Mon, 19 Feb 2018 12:39:02 +0000 Subject: [PATCH] Init meta.rel_name_active/default only if fields created --- parler/managers.py | 23 +++++++++++++---------- parler/models.py | 37 +++++++++++++++++++------------------ 2 files changed, 32 insertions(+), 28 deletions(-) diff --git a/parler/managers.py b/parler/managers.py index 7ec27366..f880bb8a 100644 --- a/parler/managers.py +++ b/parler/managers.py @@ -50,19 +50,19 @@ def __init__(self, *args, **kwargs): self._language = None def select_related(self, *fields): - fields_to_add = [] - fields_to_exclude = [] + fields_to_add = set() + fields_to_exclude = set([None]) # if rel_name_active, rel_name_default is None for extension in self.model._parler_meta: if extension.rel_name in fields: - fields_to_exclude.append(extension.rel_name) # Can not select related OneToMany field - fields_to_add.append(extension.rel_name_active) - fields_to_add.append(extension.rel_name_default) + fields_to_exclude.add(extension.rel_name) # Can not select related OneToMany field + fields_to_add.add(extension.rel_name_active) + fields_to_add.add(extension.rel_name_default) if extension.rel_name_active in fields: - fields_to_add.append(extension.rel_name_default) + fields_to_add.add(extension.rel_name_default) if extension.rel_name_default in fields: - fields_to_add.append(extension.rel_name_active) - fields = [field for field in fields if field not in fields_to_exclude] + fields_to_add - return super(TranslatableQuerySet, self).select_related(*tuple(set(fields))) + fields_to_add.add(extension.rel_name_active) + fields = set(fields).union(fields_to_add).difference(fields_to_exclude) + return super(TranslatableQuerySet, self).select_related(*tuple(fields)) def _clone(self, klass=None, setup=False, **kw): if django.VERSION < (1, 9): @@ -83,8 +83,11 @@ def _add_active_default_select_related(self): existing, defer = self.query.deferred_loading related_to_add = set() for extension in self.model._parler_meta: - if extension.rel_name: + if not extension.rel_name: + continue + if extension.rel_name_active: related_to_add.add(extension.rel_name_active) + if extension.rel_name_default: related_to_add.add(extension.rel_name_default) if defer: related_to_add = related_to_add.difference(existing) diff --git a/parler/models.py b/parler/models.py index f4c17942..c8e466e3 100644 --- a/parler/models.py +++ b/parler/models.py @@ -145,7 +145,10 @@ def create_translations_composite_fk(shared_model, related_name, translated_mode """ if not (1, 8) <= django.VERSION < (2, 0): return + meta = shared_model._parler_meta._get_extension_by_related_name(related_name) + meta.rel_name_active = related_name + '_active' + meta.rel_name_default = related_name + '_default' translations_active = CompositeOneToOneVirtualField( translated_model, null=True, @@ -569,21 +572,20 @@ def _get_translated_model(self, language_code=None, use_fallback=False, auto_cre pass else: # 2.3 from select related data or fetch from database if not (for django >=1.8, <2.0) - if (1, 8) <= django.VERSION < (2, 0): - if language_code == appsettings.PARLER_LANGUAGES.get_default_language(): - try: - object = getattr(self, meta.rel_name_default) or MISSING - except meta.model.DoesNotExist: - object = MISSING - elif language_code == self._creation_current_language: - try: - object = getattr(self, meta.rel_name_active) or MISSING - except meta.model.DoesNotExist: - object = MISSING - # Double check. It should be almost always object.language_code = language_code - # not hits only if during the queryset iteration active language has changed - if object and object.language_code != language_code: - object = None + if language_code == appsettings.PARLER_LANGUAGES.get_default_language() and meta.rel_name_default: + try: + object = getattr(self, meta.rel_name_default) or MISSING + except meta.model.DoesNotExist: + object = MISSING + elif language_code == self._creation_current_language and meta.rel_name_active: + try: + object = getattr(self, meta.rel_name_active) or MISSING + except meta.model.DoesNotExist: + object = MISSING + # Double check. It should be almost always object.language_code = language_code + # not hits only if during the queryset iteration active language has changed + if object and object.language_code != language_code: + object = None # 2.4, fetch from database if object is None: try: @@ -1158,9 +1160,8 @@ def __init__(self, shared_model, translations_model, related_name): self.shared_model = shared_model self.model = translations_model self.rel_name = related_name - if related_name: - self.rel_name_active = related_name + '_active' - self.rel_name_default = related_name + '_default' + self.rel_name_active = None + self.rel_name_default = None def get_translated_fields(self): """