title: Django: Overriding the save() method: how do I call the delete() method of a child class
body: <p>The setup = </p>
  
  <p>I have this class, Transcript:</p>
  
  <pre><code>class Transcript(models.Model):    
      body = models.TextField('Body')
      doPagination = models.BooleanField('Paginate')
      numPages = models.PositiveIntegerField('Number of Pages')
  </code></pre>
  
  <p>and this class, TranscriptPages(models.Model):</p>
  
  <pre><code>class TranscriptPages(models.Model):
      transcript = models.ForeignKey(Transcript)
      order = models.PositiveIntegerField('Order')
      content = models.TextField('Page Content', null=True, blank=True)
  </code></pre>
  
  <p>The Admin behavior I’m trying to create is to let a user populate Transcript.body with the entire contents of a long document and, if they set Transcript.doPagination = True and save the Transcript admin, I will automatically split the body into n Transcript pages.</p>
  
  <p>In the admin, TranscriptPages is a StackedInline of the Transcript Admin.</p>
  
  <p>To do this I’m overridding Transcript’s save method:</p>
  
  <pre><code>def save(self):
      if self.doPagination:
          #do stuff
          super(Transcript, self).save()
      else:
          super(Transcript, self).save()
  </code></pre>
  
  <p>The problem = </p>
  
  <p>When Transcript.doPagination is True, I want to manually delete all of the TranscriptPages that reference this Transcript so I can then create them again from scratch.</p>
  
  <p>So, I thought this would work:</p>
  
  <pre><code>#do stuff   
  TranscriptPages.objects.filter(transcript__id=self.id).delete()
  super(Transcript, self).save()
  </code></pre>
  
  <p>but when I try I get this error:</p>
  
  <blockquote>
    <p>Exception Type: ValidationError 
    Exception Value: [u'Select a valid
    choice. That choice is not one of the
    available choices.']</p>
  </blockquote>
  
  <p>... and this is the last thing in the stack trace before the exception is raised:</p>
  
  <blockquote>
    <p>.../django/forms/models.py in save_existing_objects</p>
    
    <ol start="627">
    <li>pk_value = form.fields[pk_name].clean(raw_pk_value)</li>
    </ol>
  </blockquote>
  
  <p>Other attempts to fix:</p>
  
  <ul>
  <li>t =
  self.transcriptpages_set.all().delete()
  (where self = Transcript from the
  save() method)   </li>
  <li>looping over t (above) and deleting each item individually    </li>
  <li>making a post_save signal on TranscriptPages that calls the delete method</li>
  </ul>
  
  <p>Any ideas?  How does the Admin do it?</p>
  
  <p>UPDATE: Every once in a while as I'm playing around with the code I can get a different error (below), but then it just <i>goes away</i> and I can't replicate it again... until the next random time.  </p>
  
  <blockquote>
    <p>Exception Type:<br>
    MultiValueDictKeyError Exception
    Value: "Key 'transcriptpages_set-0-id'
    not found in "
    Exception Location:
      .../django/utils/datastructures.py in
    <strong>getitem</strong>, line 203</p>
  </blockquote>
  
  <p>and the last lines from the trace:</p>
  
  <blockquote>
    <p>.../django/forms/models.py in _construct_form</p>
    
    <ol start="737">
    <li>form = super(BaseInlineFormSet, self)._construct_form(i, **kwargs) </li>
    </ol>
    
    <p>.../django/utils/datastructures.py in <strong>getitem</strong></p>
    
    <ol start="475">
    <li>pk = self.data[pk_key]</li>
    </ol>
  </blockquote>