Problem solved with BaseInlineFormSet.clean() :-)
On 05/11/2013 06:56 PM, Roberto López López wrote: > > Hi everyone, > > I need to do some validation in my model. So far, I have been able to > validate normal forms, but I want to validate forms with an inline, to > ensure that at least one of those inlines match the requirement. > > My code: > > class PDMAdminForm(ModelForm): > class Meta: > model = PersonDepartmentMembership > > def clean(self): > previous_leaders = [] > if self.instance: # the instance is already on the db > previous_leaders = > self.instance.department.employee_memberships.filter(lead__exact=True) > lead = self.cleaned_data['lead'] # access fields from the > form itself > # do some validations and eventually throw > ValidationErrrors [...] > return self.cleaned_data > > This works great for a single inline. But if I have several > PersonDepartmentMembership instances to be created, I don't know how > to check the value of those other (I just want to ensure that at least > one fo them has lead==True). > > Does anyone know how to do this cross validation? Thank you very much > for your suggestions. > > Regards, > > Roberto > > > -- > You received this message because you are subscribed to the Google > Groups "Django users" group. > To unsubscribe from this group and stop receiving emails from it, send > an email to django-users+unsubscr...@googlegroups.com. > To post to this group, send email to django-users@googlegroups.com. > Visit this group at http://groups.google.com/group/django-users?hl=en. > For more options, visit https://groups.google.com/groups/opt_out. > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users?hl=en. For more options, visit https://groups.google.com/groups/opt_out.