2

I'm trying to do this works

admin.py

class TapasInline(TranslatableStackedInline):
    model = Tapa
    can_delete = True
    extra = 0
    verbose_name = 'Tapas'
    verbose_name_plural = 'Tapas'
    fields = ('name','description','photo', 'tags')

...

class BarAdmin(TranslatableAdmin):
    inlines = (TapasInline,)

...

admin.site.register(Bar,BarAdmin)

models.py

class Tapa(TranslatableModel):
    translations = TranslatedFields(
        name = models.CharField(max_length=255,verbose_name='Nombre de la tapa'),
        description = models.TextField(verbose_name='Descripcion de la tapa')
    )
    photo = models.ImageField(verbose_name='Foto de la tapa')
    average_rating = models.FloatField(verbose_name='Puntuación media de la tapa',default=-1)
    bar = models.ForeignKey(Bar,verbose_name='Bar')
    tags = models.ManyToManyField(Tag,verbose_name='Etiquetas')
    def __unicode__(self):
        return self.lazy_translation_getter('name')

,but I'm getting this error :

hvad.exceptions.WrongManager: To access translated fields like 'name' from an untranslated model, you must use a translation aware manager. For non-translatable models, you can get one using hvad.utils.get_translation_aware_manager.
For translatable models, use the language() method.

[Django==1.8]

What am I doing wrong? How can I solve it?

Thanks in advance

oz123
  • 27,559
  • 27
  • 125
  • 187

1 Answers1

2

Unfortunately, the direct use of translated fields in admin options is not supported yet. It will be in next release (for most of them).

The culprit code is in the admin's system checks module. It would work, but the system check included in admin really insists that it will not allow a field it does not recognize.

In the meantime, you can work around the admin check by using a get_fields method instead of a fields attribute. This should do the trick:

def get_fields(self, request, obj=None):
    return ('name','description','photo', 'tags')

Please tell me if it works. I'd have answered sooner, but I don't hang around here much.

spectras
  • 13,105
  • 2
  • 31
  • 53
  • "next release" - means its out now? how can I check? – patroqueeet Jun 28 '16 at 08:26
  • Some of them work, some of them do not, the best way to check is to try. Relevant ticket is [**here**](https://github.com/KristianOellegaard/django-hvad/issues/187). For those that do not work, the workaround in this answer usually does the trick. For those that don't, it usually means there is some ambiguity in having a translatable field in that option, and it must be solved by overriding explicitly. – spectras Jun 28 '16 at 14:20
  • yes, this is what I ended up with in 100% of all cases: using `get_fields`. – patroqueeet Jun 28 '16 at 19:45