Many-To-Many Relationship changes not accessible in Model.save() ?

41 views
Skip to first unread message

Tobias Dacoir

unread,
Jan 12, 2015, 4:06:26 PM1/12/15
to django...@googlegroups.com
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.



Lachlan Musicman

unread,
Jan 12, 2015, 4:14:49 PM1/12/15
to django...@googlegroups.com
Is that related to this bug?

https://code.djangoproject.com/ticket/8892
https://code.djangoproject.com/ticket/10811

In short - if FK fields aren't saved as objects themselves first, then
they aren't correctly added to the new objects?

I had trouble with this once. Maybe put in a save after the get_or_creates?

cheers
L.
------
"This is a profound psychological violence here. How can one even
begin to speak of dignity in labor when one secretly feels one's job
should not exist?"

On the Phenomenon of Bullshit Jobs, David Graeber
http://strikemag.org/bullshit-jobs/
> --
> 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 django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> 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.

Tobias Dacoir

unread,
Jan 12, 2015, 5:00:54 PM1/12/15
to django...@googlegroups.com
Thanks for the links. It might be related to that bug. In my scripts I added another save() and then it works of course and as for the Admin Panel, until I have a solution I put a message into the help text that users have to click 'save and continue' first and then 'save' afterwards. Then it's fine as well. Phew at least the workaround was quickly implemented, still I spend at least 1 hour on figuring out what's going wrong.

Collin Anderson

unread,
Jan 14, 2015, 8:58:18 AM1/14/15
to django...@googlegroups.com
Hi,

The admin first calls obj.save(), then it saves the related data.

You could try putting your logic in ModeAdmin.save_related()

Collin
Reply all
Reply to author
Forward
0 new messages