简体   繁体   中英

django modelform inheritance

I've run into a django error that's got tearing out my hair. Background: I have a set of models inheriting from each other, and I'm trying to build a set of forms with a parallel structure.

Here's the base type for an object creation form:

class CreateSharedObjectForm(ModelForm): 
  def save(self, status, object_type, commit=True, *args, **kwargs):
    print "*********Got here!!!**************"
    shared_object = super(ModelForm,self).save( commit=False, *args, **kwargs)
    shared_object.status = status
    shared_object.object_type = object_type

    if commit:
      shared_object.save()
    return shared_object

Here's an inherited form type:

class NewBatchForm(CreateSharedObjectForm):
  def save(self, status, object_type, batch_options, commit=True, *args, **kwargs):
    print "Checkpoint A"
    batch = super(CreateSharedObjectForm,self).save( status, object_type, commit=False, *args, **kwargs )
    print "Checkpoint B"

    if commit:
      batch.save(*args, **kwargs)
    return analysis

  class Meta:
    model = batch

I call the inherited type from a view script:

 form = NewAnalysisForm(request.POST, request.FILES)
  new_analysis = form.save(
    status = 'X',
    object_type  = 'Batch',
    batch_type = 'temp',
  )

And it throws this error:

save() takes at most 2 non-keyword arguments (4 given)

If I change the "super" line to this:

batch = super(CreateSharedObjectForm,self).save( status, object_type, commit=False, *args, **kwargs )

I get this error:

Exception Type:     IntegrityError
Exception Value:    null value in column "parent_project_id" violates not-null constraint

Even wierder, django's trace output gives me this:

Checkpoint A
Checkpoint B

Before returning a HTTP 500 error.

As far as I can tell, the super line in the save method in NewBatchForm is never calling CreateSharedObjectForm. I'm aware that the super method can be tricky , but this is just single inheritance, and I can't figure out why the method for the superclass never gets called.

What's going on here? How do I fix it?

are you sure you don't want super(NewBatchForm, self).save inside NewBatchForm ?

(you have super(CreateSharedObjectForm, self)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM