在Django ModelForm中覆盖save方法。

13 浏览
0 Comments

在Django ModelForm中覆盖save方法。

我在覆盖一个ModelForm的save方法时遇到了困难。这是我收到的错误信息:

异常类型:TypeError

异常值:save()收到了一个意外的关键字参数'commit'

我的意图是让一个表单提交3个字段的多个值,然后为每个字段的组合创建一个对象,并保存每个对象。对于正确的方向,任何有帮助的指引都将非常赞。

文件models.py:

class CallResultType(models.Model):

id = models.AutoField(db_column='icontact_result_code_type_id', primary_key=True)

callResult = models.ForeignKey('CallResult', db_column='icontact_result_code_id')

campaign = models.ForeignKey('Campaign', db_column='icampaign_id')

callType = models.ForeignKey('CallType', db_column='icall_type_id')

agent = models.BooleanField(db_column='bagent', default=True)

teamLeader = models.BooleanField(db_column='bTeamLeader', default=True)

active = models.BooleanField(db_column='bactive', default=True)

文件forms.py:

from django.forms import ModelForm, ModelMultipleChoiceField

from callresults.models import *

class CallResultTypeForm(ModelForm):

callResult = ModelMultipleChoiceField(queryset=CallResult.objects.all())

campaign = ModelMultipleChoiceField(queryset=Campaign.objects.all())

callType = ModelMultipleChoiceField(queryset=CallType.objects.all())

def save(self, force_insert=False, force_update=False):

for cr in self.callResult:

for c in self.campain:

for ct in self.callType:

m = CallResultType(self) # 这一行可能是错误的

m.callResult = cr

m.campaign = c

m.calltype = ct

m.save()

class Meta:

model = CallResultType

文件admin.py:

class CallResultTypeAdmin(admin.ModelAdmin):

form = CallResultTypeForm

0