def filter_queryset_language(request, queryset):
    language = getattr(request, 'LANGUAGE_CODE')
    model = queryset.model
    if translation_pool.is_registered(model):
        info = translation_pool.get_info(model)
        filter_expr = '%s__%s' % (info.translation_join_filter, info.language_field)
    if translation_pool.is_registered_translation(model):
        info = translation_pool.get_info(model)
        filter_expr = '%s' % info.language_field
    if filter_expr:
        queryset = queryset.filter( \
            **{filter_expr: language}).distinct()
    return queryset
Beispiel #2
0
def filter_queryset_language(request, queryset):
    language = getattr(request, 'LANGUAGE_CODE', None)

    if not language:
        return queryset

    model = queryset.model
    filter_expr = None
    if translation_pool.is_registered(model):
        info = translation_pool.get_info(model)
        filter_expr = '%s__%s' % (info.translation_join_filter, info.language_field)
    if translation_pool.is_registered_translation(model):
        info = translation_pool.get_info(model)
        filter_expr = '%s' % info.language_field
    if filter_expr:
        queryset = queryset.filter( \
            **{filter_expr: language}).distinct()

    return queryset
    def process_view(self, request, view_func, view_args, view_kwargs):
        language = None
        if "language_code" in view_kwargs:
            # get language and set tralslation
            language = view_kwargs.pop("language_code")
            translation.activate(language)
            request.LANGUAGE_CODE = translation.get_language()

        if not language:
            if not self.has_language_fallback_middlewares():
                super(MultilingualGenericsMiddleware, self).process_request(request)
            language = getattr(request, "LANGUAGE_CODE")

        if "queryset" in view_kwargs:
            filter_expr = None
            model = view_kwargs["queryset"].model
            if translation_pool.is_registered(model):
                info = translation_pool.get_info(model)
                filter_expr = "%s__%s" % (info.translation_join_filter, info.language_field)
            if translation_pool.is_registered_translation(model):
                info = translation_pool.get_info(model)
                filter_expr = "%s" % info.language_field
            if filter_expr:
                view_kwargs["queryset"] = view_kwargs["queryset"].filter(**{filter_expr: language}).distinct()