Created
February 9, 2012 13:42
-
-
Save dericcrago/1780082 to your computer and use it in GitHub Desktop.
Form Validations with Instances
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from tastypie.validation import FormValidation | |
class InstanceFormValidation(FormValidation): | |
def is_valid(self, bundle, request=None): | |
errors = {} | |
data = bundle.data | |
instance = bundle.obj | |
if data is None: | |
data = {} | |
form = self.form_class(data, instance=instance) | |
if not form.is_valid(): | |
errors.update(form.errors) | |
return errors | |
class InstanceCleanedDataFormValidation(FormValidation): | |
def is_valid(self, bundle, request=None): | |
errors = {} | |
data = bundle.data | |
instance = bundle.obj | |
if data is None: | |
data = {} | |
form = self.form_class(data, instance=instance) | |
if form.is_valid(): | |
bundle.data = form.cleaned_data | |
else: | |
errors.update(form.errors) | |
return errors |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment