2011-11-28 218 views
0

我碰到一個django錯誤,它已經撕掉了我的頭髮。背景:我有一組模型彼此繼承,我試圖構建一組具有並行結構的表單。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 

即使wierder,Django的跟蹤輸出給了我這樣的:

Checkpoint A 
Checkpoint B 

在返回HTTP 500錯誤之前。

據我所知,NewBatchForm中save方法中的超級行從不調用CreateSharedObjectForm。我知道super method can be tricky,但這只是單一的繼承,我不知道爲什麼超類從未調用的方法。

這是怎麼回事?我如何解決它?

回答

1

你確定你不想super(NewBatchForm, self).save裏面NewBatchForm

(你有super(CreateSharedObjectForm, self)

+0

我嘗試過了,它拋出:保存()有多個值的關鍵字參數「提交」 – Abe

+0

感謝好了,快回答我無法相信我錯過了我自己。 – Abe