ziima / django-multilingual-ds9

Yet another branch of django-multilingual with improved and legible core.
MIT License
10 stars 2 forks source link

Entries appearing more than once if translations are present #12

Open guzru opened 11 years ago

guzru commented 11 years ago

When translations are added to any translated field, the main object appears once for every translation in the results. It happens in the admin too: I can see eg. 2 entries referring to the same object id, and they are displayed with the same translation, so that it looks like a duplicate record.

I tried to solve with a custom manager but I get errors here and there.

This is my code:

class ProfessionManager(MultilingualManager):
    def filter(self, *args, **kwargs):
        # Get base queryset, caching translations
        qs = self.get_query_set().select_related('translations')
        # Check if there's a language filter
        if not kwargs.get('translations__language_code'):
            kwargs['translations__language_code'] = get_language()[0:2]

        qs = qs.filter(*args, **kwargs)
        return qs

    def all(self):
        return self.filter()

class Profession(models.Model):
    objects = ProfessionManager()

    class Meta:
        ordering = ('translations__name',)

    class Translation:
        name = models.CharField(_('name'), max_length=200)
        description = models.TextField(_('description'),blank=True,null=True)

    def __unicode__(self):
        return u"%s" % (self.name_any)

This is causing me problems when I attach this models as a m2m, because related objects multiply as soon as I add translations.

Is this by design, and if so, is there any method you suggest to solve it?

Thanks!

guzru commented 11 years ago

selected_related('translations') is wrong, sorry. But the issue remains.

ziima commented 11 years ago

What kind of queries does the problem? I can not find any unusual behavior for standard queries.