Browse Source

defer updating the properties until after model.save() has been called when commit is False

Natenom/support-murmur-13-1446181288462
Michael Ziegler 15 years ago
parent
commit
366be735cb
  1. 18
      pyweb/mumble/forms.py

18
pyweb/mumble/forms.py

@ -33,7 +33,6 @@ class PropertyModelForm( ModelForm ):
def __init__( self, *args, **kwargs ): def __init__( self, *args, **kwargs ):
ModelForm.__init__( self, *args, **kwargs ); ModelForm.__init__( self, *args, **kwargs );
instfields = self.instance._meta.get_all_field_names() instfields = self.instance._meta.get_all_field_names()
for fldname in self.fields: for fldname in self.fields:
@ -46,14 +45,27 @@ class PropertyModelForm( ModelForm ):
def save( self, commit=True ): def save( self, commit=True ):
inst = ModelForm.save( self, commit=commit ) inst = ModelForm.save( self, commit=commit )
if commit:
self.save_to_model( inst )
else:
# Update when the model has been saved.
from django.db.models import signals
self._update_inst = inst
signals.post_save.connect( self.save_listener, sender=inst.__class__ )
return inst
def save_listener( self, **kwargs ):
if kwargs['instance'] is self._update_inst:
self.save_to_model( self._update_inst )
def save_to_model( self, inst ):
instfields = inst._meta.get_all_field_names() instfields = inst._meta.get_all_field_names()
for fldname in self.fields: for fldname in self.fields:
if fldname not in instfields: if fldname not in instfields:
setattr( inst, fldname, self.cleaned_data[fldname] ) setattr( inst, fldname, self.cleaned_data[fldname] )
return inst
class MumbleForm( PropertyModelForm ): class MumbleForm( PropertyModelForm ):
""" The Mumble Server admin form that allows to configure settings which do """ The Mumble Server admin form that allows to configure settings which do

Loading…
Cancel
Save