开发者

Django: Overriding the save() method: how do I call the delete() method of a child class

开发者 https://www.devze.com 2022-12-22 11:58 出处:网络
The setup = I have this class, Transcript: class Transcript(models.Model): body = models.TextField(\'Body\')

The setup =

I have this class, Transcript:

class Transcript(models.Model):    
    body = models.TextField('Body')
    doPagination = models.BooleanField('Paginate')
    numPages = models.PositiveIntegerField('Number of Pages')

and this class, TranscriptPages(models.Model):

class TranscriptPages(models.Model):
    transcript = models.ForeignKey(Transcript)
    order = models.PositiveIntegerField('Order')
    content = models.TextField('Page Content', null=True, blank=True)

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.

In the admin, TranscriptPages is a StackedInline of the Transcript Admin.

To do this I’m overridding Transcript’s save method:

def save(self):
    if self.doPagination:
        #do stuff
        super(Transcript, self).save()
    else:
        super(Transcrip开发者_StackOverflowt, self).save()

The problem =

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.

So, I thought this would work:

#do stuff   
TranscriptPages.objects.filter(transcript__id=self.id).delete()
super(Transcript, self).save()

but when I try I get this error:

Exception Type: ValidationError Exception Value: [u'Select a valid choice. That choice is not one of the available choices.']

... and this is the last thing in the stack trace before the exception is raised:

.../django/forms/models.py in save_existing_objects

  1. pk_value = form.fields[pk_name].clean(raw_pk_value)

Other attempts to fix:

  • t = self.transcriptpages_set.all().delete() (where self = Transcript from the save() method)
  • looping over t (above) and deleting each item individually
  • making a post_save signal on TranscriptPages that calls the delete method

Any ideas? How does the Admin do it?

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 goes away and I can't replicate it again... until the next random time.

Exception Type:

MultiValueDictKeyError Exception Value: "Key 'transcriptpages_set-0-id' not found in " Exception Location: .../django/utils/datastructures.py in getitem, line 203

and the last lines from the trace:

.../django/forms/models.py in _construct_form

  1. form = super(BaseInlineFormSet, self)._construct_form(i, **kwargs)

.../django/utils/datastructures.py in getitem

  1. pk = self.data[pk_key]


In the end it was a matter of timing. When deleting only a single child object out of many, there was no problem. If I was deleting too many child objects at once, the error could happen, because the delete action was attempting to reference ids that were not around. This is why nothing worked, not signals, not [object]_set. I fixed it by using jquery to set a hidden variable in the edit form for the child object, which caused the object to first process the update (slowing down it's processing) and THEN the delete.


You are probably trying to access the Transaction.id before is has been created. Additionally, you can try to access the TransactionPage objects through the Transaction object via transactionpage_set (see query docs about FOO_set notation).

def save(self):
    super(Transcript, self).save()
    if self.doPagination:
        self.transaction_set.all().delete() # .all() may be optional
        pages = ... # whatever you do to split the content
        self.numPages = len(pages)
        self.save() # yes, a second save if you store numPages in Transaction
        for page_number in range(self.numPages):
            TransactionPage.objects.create(content=pages[page_number], 
                                           order=page_number, transaction=self)

You could also switch to not storing numPages in the Transaction and access it via a property instead.

class Transaction(models.Model):
    # ...
    # replace numPages with
    @property
    def page_count(self):
        return self.transactionpage_set.count() or 1

And then if you took it one step further you could always use the TransactionPage objects for display purposes. This would allow you to get rid of the extra self.save() call in the above save() method. It will also let you simplify your templates by always displaying TransactionPage.content instead of conditionally displaying Transaction.body if you are paginating and TransactionPage.content otherwise.

class Transaction(models.Model):
    body = models.TextField()
    paginate = models.BooleanField()

    @property
    def page_count(self):
        return self.transactionpage_set.count() # no more "or 1" cruft!

    def save(self):
        super(Transcript, self).save()
        self.transaction_set.delete() # might need an .all() before .delete()
        if self.paginate:
            # Do whatever you do to split the body into pages.
            pages = ... 
        else:
            # The only page is the entire body.
            pages = [self.body]
        for (page_number, page_content) in enumerate(pages):
            TransactionPage.objects.create(content=page_content, 
                                           order=page_number, transaction=self)


Another possible solution might be to override save_formset() in the ModelAdmin to prevent the update completely:

def save_formset(self, request, form, formset, change):
     if (form.cleaned_data['do_pagination'] and 
            formset.model == TranscriptPages):
        formset.changed_objects = []
        formset.new_objects = []
        formset.deleted_objects = []
    else:
        formset.save()

Then, you can do whatever you like in Modal.save(). Note that there's probably a more elegant/breakproof way to stop the formset from processing if one wanted to dig into the internals a bit more.

0

精彩评论

暂无评论...
验证码 换一张
取 消