Django model form automatically validate own form while we create form, but some time we add extra field and need validate customisation at this time need to call validation function “clean” for this type of conditions, i have try with following code, you can also try :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
class ProfileForm(ModelForm): class Meta: widgets = { # 'about': AutosizedTextarea, # You can also specify html attributes 'about': AutosizedTextarea(attrs={'rows': 3, 'class': 'input-xlarge'}), 'first_name': EnclosedInput(prepend='icon-user'), 'birthdate': SuitDateWidget, } def clean(self): company = self.cleaned_data.get("company") all_user = UserProfile.objects.filter(company=company) for user in all_user: usr = User.objects.get(email=user.email) user_groups = usr.groups.all() for ug in user_groups: if "Manager" == str(ug.name): print 'all match' print ug.name, "Manager" msg = _('Manager already exits in this company !') raise forms.ValidationError(msg) break else: pass else: pass else: pass # raise forms.ValidationError("Dates are fucked up") return self.cleaned_data |