django modelform继承

时间:2011-11-28 21:47:46

标签: python django inheritance django-forms super

我遇到了一个django错误,它已经撕掉了我的头发。背景:我有一组相互继承的模型,我正在尝试构建一组具有并行结构的表单。

以下是对象创建表单的基本类型:

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

这是一个继承的表单类型:

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

我从视图脚本调用继承的类型:

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

它抛出了这个错误:

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

如果我将“超级”行更改为:

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

我收到此错误:

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

更奇怪的是,django的跟踪输出给了我这个:

Checkpoint A
Checkpoint B

在返回HTTP 500错误之前。

据我所知,NewBatchForm中save方法中的超级行永远不会调用CreateSharedObjectForm。我知道super method can be tricky,但这只是单继承,我无法弄清楚为什么超类从不的方法被调用。

这里发生了什么?我该如何解决?

1 个答案:

答案 0 :(得分:1)

您确定super(NewBatchForm, self).save内有NewBatchForm吗?

(您有super(CreateSharedObjectForm, self)