'In Django remove duplicates of a QuerySet when using parler
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?
Solution 1:[1]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jota |
