Advertisement
Guest User

finsprings

a guest
Aug 12th, 2009
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.33 KB | None | 0 0
  1. class FooAdminForm(forms.ModelForm):
  2.     json_serializer = serializers.get_serializer("json")()
  3.    
  4.     # TODO: make it hide the colon before our empty label
  5.     # TODO: figure out how not to break encapsulation to set the value
  6.     orig_model_json = forms.CharField(widget=forms.HiddenInput, label="")
  7.    
  8.    
  9.     def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None,
  10.                  initial=None, error_class=ErrorList, label_suffix=':',
  11.                  empty_permitted=False, instance=None):
  12.         super(CaseAdminForm, self).__init__(data, files, auto_id, prefix, initial,
  13.                                          error_class, label_suffix, empty_permitted, instance)
  14.         if instance:
  15.             omj = self.fields["orig_model_json"]
  16.             omj.initial = self.json_serializer.serialize((instance,), ensure_ascii=False)            
  17.        
  18.     class Meta:
  19.         model = Foo
  20.  
  21.     def clean(self):
  22.         omj = self.fields["orig_model_json"]
  23.         print "omj:",omj.initial
  24.         if omj.initial:
  25.             current_model_json = self.json_serializer.serialize((self.instance,), ensure_ascii=False)
  26.             if current_model_json != omj.initial:
  27.                 raise forms.ValidationError("Record has changed since you started editing it.")
  28.            
  29.         return self.cleaned_data
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement