﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
19731	previous values of ManyToManyField in model's save method	s4mmael@…	nobody	"Hello,

I'm terribly sorry for bothering you, but I believe this behavior may be incorrect. I've already asked at django-users mailing list and I've had no answer.

How to reproduce it:



{{{
    class Tag(models.Model):
        name = models.CharField(max_length=50, unique=True, db_index=True)
        parent = ForeignKey('self', null=True, blank=True, related_name='children')

    class Album(models.Model):
        name = models.CharField(max_length=64, blank=True)
        tags = models.ManyToManyField(Tag, blank=True)
        def tags_(self):
            return ', '.join([t.name for t in self.tags.all()])
        def save(self, *args, **kwargs):
            super(Album, self).save(*args, **kwargs)
            f = open('/tmp/test.txt', 'wt')
            f.write('%s\n%s\n' % (self.name, self.tags_()))
            f.close
}}}
Now let's go to django admin and create two tags: tag1, tag2. After that let's create an album 'Album_1' with tag 'tag1' and save the album. In the file /tmp/test.txt we see:

----
Album_1

----
 

Let's add tag2 to the album, change it's name to Album_2 and save. The file will be:

----
Album_2
tag1
----

Let's remove both tags and save:

----
Album_2
tag1, tag2
----

Let's save it again:

----
Album_2

----

So, while everything looks good in Django admin, we get old values of album.tags_() method in Album.save(), even if we call super(Album, self).save(*args, * *kwargs) before.

I've also tried to use signals like this:


{{{
    @receiver(post_save, sender=Album)
    def album_save_handler(sender, instance, **kwargs):
        f = open('/tmp/test.txt', 'wt')
        f.write('%s\n%s\n' % (instance.name, instance.tags_()))
        f.close

}}}

And even like this:


{{{
    @receiver(post_save, sender=Album)
    def album_save_handler(sender, instance, **kwargs):
        t = Album.objects.get(pk = instance.id).tags_()
        f = open('/tmp/test.txt', 'wt')
        f.write('%s\n%s\n' % (instance.name, t))
        f.close

}}}

Still no luck, results are the same: previous tags instead of the current ones.
"	Uncategorized	closed	Database layer (models, ORM)	1.4	Normal	invalid	manytomanyfield,save,previous		Unreviewed	0	0	0	0	0	0
