In Django remove duplicates of a QuerySet when using parler

508 Views Asked by At

I have a model like this:

from django.db import models
from django.utils.translation import ugettext_lazy as _

from parler.models import TranslatableModel, TranslatedFields

...

class UnitNode(TranslatableModel):
    ...
    translations = TranslatedFields(
        title=models.CharField(_(u'title'), max_length=1024),
        slug=models.SlugField(_('slug'))
        ),
    )

...

I want a QuerySet of UnitNodes without duplicates, sorted by slug. When I query something like this:

qs = UnitNode.objects.distinct().order_by("translations__slug")

I get duplicates.

How do I get rid of duplicates?

1

There are 1 best solutions below

1
On

The solution is:

from django.utils.translation import get_language
Country.objects.translated(get_language()).order_by("translations__name")

This way you only get the instances for the current language in use in the app and it won't generate duplicates. Just as you were doing it gets all instances of all languages ​​by duplicating itself in different languages.