10
10
from django.core.exceptions import FieldError
11
11
from django.db import connection
12
12
from django.db.models.fields import FieldDoesNotExist
13
from django.db.models.query import QuerySet, Q
13
from django.db.models.query import QuerySet, Q, ValuesQuerySet
14
14
from django.db.models.sql.query import Query
15
15
from django.db.models.sql.datastructures import (
580
580
return super(MultilingualModelQuerySet, self).order_by(*field_names)
582
582
def values(self, *fields):
583
raise NotImplementedError
583
if hasattr(self.model._meta, 'translation_model'):
585
trans_opts = self.model._meta.translation_model._meta
586
trans_table_name = trans_opts.db_table
587
qn2 = self.query.connection.ops.quote_name
589
for field_name in fields:
590
field_and_lang = trans_opts.translated_fields.get(field_name)
592
field, language_id = field_and_lang
593
if language_id is None:
594
language_id = getattr(self, '_default_language', None)
595
table_alias = get_translation_table_alias(trans_table_name,
597
extra_select[field_name] = qn2(table_alias) + '.' + qn2(field.attname)
599
# this maps columns to required field_names
600
result = self.extra(select = extra_select)
601
# and it returns MultilingualModelQuerySet instance, so we have to super it
602
return super(MultilingualModelQuerySet, result).values(*fields)
604
return super(MultilingualModelQuerySet, self).values(*fields)
585
606
def values_list(self, *fields, **kwargs):
586
raise NotImplementedError
607
if hasattr(self.model._meta, 'translation_model'):
609
trans_opts = self.model._meta.translation_model._meta
610
trans_table_name = trans_opts.db_table
611
qn2 = self.query.connection.ops.quote_name
613
for field_name in fields:
614
field_and_lang = trans_opts.translated_fields.get(field_name)
616
field, language_id = field_and_lang
617
if language_id is None:
618
language_id = getattr(self, '_default_language', None)
619
table_alias = get_translation_table_alias(trans_table_name,
621
extra_select[field_name] = qn2(table_alias) + '.' + qn2(field.attname)
623
# this maps columns to required field_names
624
result = self.extra(select = extra_select)
625
# and it return MultilingualModelQuerySet instance, so we have to super it
626
return super(MultilingualModelQuerySet, result).values_list(*fields, **kwargs)
628
return super(MultilingualModelQuerySet, self).values_list(*fields, **kwargs)