python - Overriding the save method in Django ModelForm -


I'm having trouble overriding a ModelForm save method. I get this error I'm:

  Exception type: TypeErier Exception Value: Save () found an unexpected keyword argument 'committed'  

My intentions include a form 3 fields Submit several values ​​for, then create an object for each combination of those areas, and to save each object

file models.py

  class collarsult type (models.Model): id = model. 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 = Django.forms import ModelForm, ModelMultipleChoiceFill from callresults from 'useless', default = true)  

file forms.py

  Field Models import * class Colrslt Taipform (Modlform): Colrslt = model Bhustriyfoisfild (Kweriset = Colrsltkobjektkol)) campaign = Modlmaltiplfaitfild (Kweriset = Abiankobji cts.all ()) CALLTYPE = ModelMultipleChoiceField (queryset = CallType.objects.all ()) def save (self, force_insert = false, force_update = false) to c self.campain: self.callType to whistle for self.callResult million: m = callResultType (self) # this line Maybe wrong is m.callResult = cr m.campaign = c m.calltype = ct m.save () Class Meta: model = callResultType  

file admin.py

  Class Colrslttaipadmin (Adminkmodeladmin): form = Callersult type  

In your save you have arguments Commit If anyone overrides your form, or save it that wants to change it, then it will do the save (commits = folose) , will modify the output , And then save it automatically.

In addition, models that save your model form should be returned. Usually save of a ModelForm will look something like this:

  def save (self, commission = true): m = super (collarsult typeform, itself). Save (commit = False) # If there is any custom stuff: read on m.save () Return M  

.

In the end, this many ModelForm will not work just because you are reaching things, instead of self.callResult , you get to self.fields [ CallResult '] .

On one side: Why not just in the model ManyToManyField s, so you do not need to do this? It seems that you are storing redundant data and are working more for yourself (and I : P ) Import from Django.db.models AutoField def copy_model_instance (obj)): Make a copy of the model example. M2M relationships are currently not handled, i.e. they are not copied. (Fortunately , You have no one in this case) See also Django # 4027. Http: // Blog.elsdoerfer.name/2008/09/09/making-a-copy-of-a-model-instance/ "" "For primitive = dict (f.name, getattr (obj, f.name)) does not exist in obj._meta.fields, then (f, AutoField) and obj._meta.parents.values ​​() ]) Return obj .__ class __ (** initial) class CallResultTypeForm (ModelF Orm): callResult = ModelMultipleChoiceField (query_group = CallResult.objects.all ()) campaign = model multilevel field (queryset = campaign.objectsl ()) call type = model multilevel field (queryset = call type.object.all) Diarf save (self, commit = True * Arges, ** Cwarj): M = Super (Cal lResultTypeForm, self) .save (commit = false, * args, ** kwargs) result = [] in self.callResult million For: in self.campain for c: for self.callType whistle: m_new = copy_model_instance (m) m_new. CallResult = million m_new.campai Gn = c m_new.calltype = ct if committed: m_new.save () results.append (m_new) return result

this allows for the legacy of CallResultTypeForm , In case of bus it is necessary.


Comments

Popular posts from this blog

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -