Tuesday, May 1, 2012

Re: Problem with a clean in inline formset

Solution:

def clean(self):
       if any(self.errors):
           # Don't bother validating the formset unless each form is
valid on its own
           return
       for i in range(0, self.total_form_count()):
           form = self.forms[i]
           cleaned_data = form.clean()
           display = cleaned_data.get('display', None)
           if cleaned_data['DELETE'] == False:
                 if display.max_windows() >= display.windows:
                   raise forms.ValidationError("The display have all
windows occupied.")

Put the line if cleaned_data['DELETE'] == False:  to check if the inline have the attribute DELETE and if is false, to ignore deleted rows in formset that have true in this attribute. =)

2012/4/30 Guevara <eguevara2012@gmail.com>
Hello!

The (if display.max_windows() >= display.windows) works fine to
prevent insert. Behaves as expected.
But is raised even when the row is REMOVED from the edit formset. =/

## Models

class Display(models.Model):
   windows = models.IntegerField()

def max_windows(self):
       total = self.window_set.all().count() #window is a
intermediary class to campaign
       if total:
           return total

## Clean method in CampaignInlineFormSet

def clean(self):
       if any(self.errors):
           # Don't bother validating the formset unless each form is
valid on its own
           return
       for i in range(0, self.total_form_count()):
           form = self.forms[i]
           cleaned_data = form.clean()
           display = cleaned_data.get('display', None)
           if display.max_windows() >= display.windows:
                   raise forms.ValidationError("The display have all
windows occupied.")

This "if" can not stop me remove a row in inline formset. How can I
fix this?
Regards.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment