. Aşağıdakileri form düzeyinde kullanmak için, clean_data kullanın.
class Article(models.Model):
status = models.CharField(max_length=75, blank=False, null=False)
pub_date = models.CharField(max_length=75, blank=False, null=False)
def clean(self):
# Don't allow draft entries to have a pub_date.
if self.status == 'draft' and self.pub_date is not None:
raise ValidationError('Draft entries may not have a publication date.')
# Set the pub_date for published items if it hasn't been set already.
if self.status == 'published' and self.pub_date is None:
self.pub_date = datetime.date.today()
Referans:
class SimpleForm(forms.ModelForm):
def clean(self):
cleaned_data = super(SimpleForm, self).clean() # Get the cleaned data from default clean, returns cleaned_data
field1 = cleaned_data.get("field1")
field2 = cleaned_data.get("field2"),
if not field1 and not field2:
raise forms.ValidationError('Please fill in both fields.')
return cleaned_data
Referans: Form & Field Validation
Not: Örneğin bir Django CMS eklentisi için bunu ise oldukça forms.py daha, eklenti için models.py tanımında temiz (öz) yöntemini geçersiz kılmak için emin olun. –