How I can access the fields without translation through an instance of TranslatableModel?

121 views Asked by At

I need the name of the project (field project, no translation) for build the path of upload_to directory in ImageField. But i get this error:

AttributeError at /admin/api/stage/add/ 'StageTranslation' object has no attribute 'project'

def stage_name_path(instance, filename):
    # get error at instance.project
    return '/'.join(filter(None, (instance.project, 'stages', filename)))

class Stage(TranslatableModel):
    name = models.CharField(
        'Short name',
        max_length=50
    )
    text = models.CharField(
        'Text',
        max_length=120
    )
    translations = TranslatedFields(
            image=models.ImageField(
            'Image (i18n)',
            upload_to=stage_name_path
        ),
    )

    project = models.ForeignKey(Project, related_name='stages')

Through the instance I can access the fields with translation (image) but I can not access the fields name, text or project... the ones who really need.

1

There are 1 answers

1
ILdar Migranov On BEST ANSWER

Try:instance.master.project. The StageTranslation has a ForeignKey field master is a reference to Sage.