Skip to content
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

Query by active lang #39

Closed
brahica opened this issue Dec 13, 2017 · 5 comments
Closed

Query by active lang #39

brahica opened this issue Dec 13, 2017 · 5 comments

Comments

@brahica
Copy link

brahica commented Dec 13, 2017

Hi,

Just asking, there is a ways filtring by active lang like :

from django.db import models
from localized_fields.fields import LocalizedCharField
from localized_fields.models import LocalizedModel
from django.utils.translation import activate

class MyModel(LocalizedModel, models.Model):
    name = LocalizedCharField(required=True)

"""
If MyModel instance are:
{
'name': {
        'en': foo,
        'es': bar
    }
}
"""

activate('en')    # Active lang 'en'
MyModel.objects.filter(name='foo')    # Find all instance that have name['en'] = 'foo'
MyModel.objects.filter(name__contains='foo')    # Find all instance that have name['en'] = '%foo%'

Thx ;)

@seroy
Copy link
Contributor

seroy commented Dec 13, 2017

LocalizedField is HStoreField with language codes as keys:

MyModel.objects.filter(name__en='foo') # Find all instance that have name['en'] = 'foo'
MyModel.objects.filter(name__en__contains='foo')    # Find all instance that have name['en'] = '%foo%'

See HStoreField Key lookups on Django Docs

@seroy
Copy link
Contributor

seroy commented Dec 13, 2017

Oops, am re-read the question. Seems currently is no way to do this.

@si14
Copy link
Contributor

si14 commented Feb 4, 2018

Hey @brahica, I would argue that's a bad design. "Active language" is an implicit thread-local state which is most often set during the request handling, so it's best to keep it contained in your views and templates. Lookup like that will break in unexpected ways if you use it in Celery or management tasks.

@sliverc
Copy link
Contributor

sliverc commented Jul 4, 2018

@si14 I guess the problem you mention we already have as localized fields depend on thread local state e.g. when calling my_model.name = 'Test' will set Test in current language.

This said I would love to see such a feature as well.

@sliverc
Copy link
Contributor

sliverc commented Jul 5, 2018

As I needed this I have created following search lookup snippet for localized fields in my projects. Potentially can be used as an example to implement other lookups as well.

from django.contrib.postgres.fields.hstore import KeyTransform
from django.contrib.postgres.search import (SearchVector, SearchVectorExact,
                                            SearchVectorField)
from django.utils import translation
from localized_fields.fields import LocalizedField


class LocalizedSearchLookup(SearchVectorExact):
    lookup_name = 'search'

    def process_lhs(self, qn, connection):
        if not isinstance(self.lhs.output_field, SearchVectorField):
            self.lhs = SearchVector(
                KeyTransform(translation.get_language(), self.lhs)
            )
        return super().process_lhs(qn, connection)

    def get_prep_lookup(self):
        return str(self.rhs)


LocalizedField.register_lookup(LocalizedSearchLookup)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants