field autopopulated from a translated field

11 views
Skip to first unread message

Manuel Quiñones

unread,
Mar 10, 2009, 3:15:31 PM3/10/09
to django-multilingual
hello

I'm tryng to do a model with a slug field, to be autopopulated from a
specific translation of another field.

something like this:


-----

from django.db import models
import multilingual
from django.template.defaultfilters import slugify

class MyModel(models.Model):
slug = models.SlugField(editable=False, max_length=30,
unique=True)

class Translation(multilingual.Translation):
name = models.CharField(max_length=30)

def save(self):
self.slug = slugify(self.name_en)
super(MyModel, self).save()

-----

the first time I save the model in the admin, the slug is set to
"none". but if I edit it and save it again, the slug is set to the
previous value of the name field in english.

this is obviously because the save method for the translation model is
called after the save method of my model.

how can I deal with this? is there a way to do what I want?

Natim

unread,
Apr 4, 2009, 6:25:20 PM4/4/09
to django-multilingual
You should define the save method for Translation.

class MyModel(models.Model):
slug = models.SlugField(editable=False, max_length=30,
unique=True)

class Translation(multilingual.Translation):
name = models.CharField(max_length=30)

def save(self):
parent = MyModel.objects.get(pk=self.master_id)
parent.slug = slugify(self.name)
parent.save()
super(MyModel.Translation, self).save()

Something like that but with a test to do it only for the en language.

Cheers

Rémy

Manuel Quiñones

unread,
Apr 11, 2009, 4:57:52 PM4/11/09
to django-multilingual


On 4 abr, 19:25, Natim <hubscher.r...@gmail.com> wrote:
> You should define the save method for Translation.
>
> class MyModel(models.Model):
>     slug = models.SlugField(editable=False, max_length=30,
> unique=True)
>
>     class Translation(multilingual.Translation):
>         name = models.CharField(max_length=30)
>
>     def save(self):
>         parent = MyModel.objects.get(pk=self.master_id)
>         parent.slug = slugify(self.name)
>         parent.save()
>         super(MyModel.Translation, self).save()
>
> Something like that but with a test to do it only for the en language.

thanks, that works. I was avoiding the relationship between the
original model and the translation model.
Reply all
Reply to author
Forward
0 new messages