I'm trying to access the value of a many-to-many relationship during the 
save() method of my model but it always returns the state it had before, 
even though the first thing I do is call super.save().

class Database(models.Model):
    ...
    questions = models.ManyToManyField(Question)
    pathToAudioFiles = models.CharField(max_length=255, unique=True, 
help_text="Please type in path to folder with Audio Files, e.g. 
media/database1/*.mp3", verbose_name="Path to Audio Files")

    def audioData(self):
        return AudioData.objects.filter(database=self.pk)

    def __unicode__(self):
        return self.name

    def save(self, *args, **kwargs):
        super(Database, self).save(*args, **kwargs)

        filelist = glob.glob(self.pathToAudioFiles)
        for file in filelist:
            print "adding file %s" % file
            audioData = AudioData.objects.get_or_create(path=file, filename=
file, database=self)

        # then update pairs
        for a in self.audioData():
            print 'working on %s' % a
            print self.questions.all()
            for q in self.questions.all():
                print "creating pairs with %s" % q
                pair = AudioQuestionPair.objects.get_or_create(audioData=a, 
question=q, database=self)
                for choice in q.choices:
                    print "adding answer %s" % choice
                    answer = Answers.objects.get_or_create(body=choice, 
audioQuestionPair=pair)

In this case, the idea is that the Admin can add a new 'database' (they 
wanted to call it that way), that contains some audio files that are stored 
somewhere on disk. so far so good. Later on, using the Django Admin Panel 
the admin modifies the questions. When I use the shell I get this output, 
which is normal:
>>> from play.models import *
>>> db = Database.objects.create(name="database1", pathToAudioFiles=
"media/database1/*.mp3")
adding file media/database1\devel_001.mp3
adding file media/database1\devel_010.mp3
working on media/database1\devel_001.mp3
[]
working on media/database1\devel_010.mp3
[]

Then I go to the Admin Panel and edit the associated questions of that 
object, and when I hit save:
[12/Jan/2015 21:57:50] "GET /admin/play/database/1/ HTTP/1.1" 200 7021
[12/Jan/2015 21:57:50] "GET /admin/jsi18n/ HTTP/1.1" 200 2372
adding file media/database1\devel_001.mp3
adding file media/database1\devel_010.mp3
working on media/database1\devel_001.mp3
[]
working on media/database1\devel_010.mp3
[]
[12/Jan/2015 21:57:56] "POST /admin/play/database/1/ HTTP/1.1" 302 0

So even though I did change the associations it's not accessible during the 
save method. It still turns up empty. How can I solve this? After hitting 
save I need to be able to process self.questions.all() right away.



-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/16f1653a-764c-4163-9c13-c661513d9321%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to