Thank you Collin, I tried that before posting in this group but the
same thing happened: I get no errors when saving but Tags still remain
not asociated with Receta.

class Receta(models.Model):
   ....
   def save(self):
        from django.template.defaultfilters import slugify
        tags = self.etiquetas_text.split(',')
        super(Receta, self).save()
        for tag in tags:
            etiq, created =
Tag.objects.get_or_create(nombre=tag,slug=slugify(tag))
            self.etiquetas.add(etiq)


Anyone knows where the problem is?

Thanks.


On 11 jul, 01:06, Collin Grady <[EMAIL PROTECTED]> wrote:
> A ManyToManyField is not a list to be replaced - add the new tags
> directly to etiquetas using the .add() method on it.
>
> On Jul 10, 9:34 am, zenx <[EMAIL PROTECTED]> wrote:
>
> > Hi,
>
> > I would like to get a list of tags separated by a comma and be able to
> > save them as related objects for the current object. Currently I have
> > this code. When saving a Receta object it creates the tags objects if
> > they doesn't exist yet and it should save the relations between Tags
> > and Receta, but they are not saved. The code doesn't give any errors
> > but the Tag objects are not linked to the Receta object after saving
> > it.
>
> > Any ideas why this happens? Thank you!!!
>
> > class Tag(models.Model):
> >     nombre = models.CharField(maxlength=20)
> >     slug = models.SlugField(prepopulate_from=("nombre",),unique=True)
>
> > class Receta(models.Model):
> >     user = models.ForeignKey(User)
> >     nombre = models.CharField(maxlength=50)
> >     slug = models.SlugField(prepopulate_from=("nombre",),unique=True)
> >     etiquetas =
> > models.ManyToManyField(Tag,related_name='recetas',blank=True)
> >     tags_text = models.CharField(maxlength=150)
>
> >     def save(self):
> >         from django.template.defaultfilters import slugify
> >         tags = self.tags_text.split(',')
> >         super(Receta, self).save()
> >         etiquetas_new = []
> >         for tag in tags:
> >             etiq, created =
> > Tag.objects.get_or_create(nombre=tag,slug=slugify(tag))
> >             etiquetas_new.append(etiq)
> >         self.etiquetas = etiquetas_new


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to