Guest User

Untitled

a guest
Jun 18th, 2013
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.82 KB | None | 0 0
  1. class CombinedFormSet(BaseInlineFormSet):
  2.     # Validate formset data here
  3.     def clean(self):
  4.         super(CombinedFormSet, self).clean()
  5.         for form in self.forms:
  6.             if not hasattr(form, 'cleaned_data'):
  7.                 continue
  8.  
  9.             data = self.cleaned_data
  10.             valid = False
  11.             for i in data:
  12.                 if i != {}:
  13.                     if i['is_correct']:
  14.                         valid = True
  15.  
  16.             if not valid:
  17.                 #TODO: translate admin?
  18.                 raise forms.ValidationError("A Question must have an answer.")
  19.  
  20.             # Always return the cleaned data, whether you have changed it or
  21.             # not.
  22.             return data
  23.  
  24.  
  25. class ChoiceInline(admin.TabularInline):
  26.     model = Choice
  27.     extra = 4
  28.     formset = CombinedFormSet
Advertisement
Add Comment
Please, Sign In to add comment