~panosl/django-multilingual/trunk-1

« back to all changes in this revision

Viewing changes to multilingual/query.py

  • Committer: Panos Laganakos
  • Date: 2011-03-04 17:35:52 UTC
  • Revision ID: panos.laganakos@gmail.com-20110304173552-kzq7is3edd7xvc8f
Fixed issue #95, thanks vlastimil.zima.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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 (
16
16
    EmptyResultSet,
580
580
            return super(MultilingualModelQuerySet, self).order_by(*field_names)
581
581
 
582
582
    def values(self, *fields):
583
 
        raise NotImplementedError
 
583
        if hasattr(self.model._meta, 'translation_model'):
 
584
            extra_select = {}
 
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
 
588
 
 
589
            for field_name in fields:
 
590
                field_and_lang = trans_opts.translated_fields.get(field_name)
 
591
                if field_and_lang:
 
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,
 
596
                        language_id)
 
597
                    extra_select[field_name] = qn2(table_alias) + '.' + qn2(field.attname)
 
598
            
 
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)
 
603
        else:
 
604
            return super(MultilingualModelQuerySet, self).values(*fields) 
584
605
 
585
606
    def values_list(self, *fields, **kwargs):
586
 
        raise NotImplementedError
 
607
        if hasattr(self.model._meta, 'translation_model'):
 
608
            extra_select = {}
 
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
 
612
 
 
613
            for field_name in fields:
 
614
                field_and_lang = trans_opts.translated_fields.get(field_name)
 
615
                if field_and_lang:
 
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,
 
620
                        language_id)
 
621
                    extra_select[field_name] = qn2(table_alias) + '.' + qn2(field.attname)
 
622
            
 
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)
 
627
        else:
 
628
            return super(MultilingualModelQuerySet, self).values_list(*fields, **kwargs) 
 
629