2011-09-08 14 views
5

में start_date से end_date मान्य है, मेरे मॉडल में start_date और end_date फ़ील्ड्स हैं, मैं start_date से बड़ा होने पर end_date को एक त्रुटि असाइन करना चाहता हूं, मैं दस्तावेज़ देख रहा हूं, लेकिन मुझे नहीं मिला इसके बारे में एक उदाहरण।डीजेगो मॉडल फॉर्म

def clean(self): 
    start_date = cleaned_data.get("start_date") 
    end_date = cleaned_data.get("end_date") 
    if end_date < start_date: 
     msg = u"End date should be greater than start date." 
     self._errors["end_date"] = self.error_class([msg]) 
+0

बंद .... यह मेरे सवाल का जवाब ... http://stackoverflow.com/questions/2117048/django-overriding-the-clean-method-in-forms-question-about- स्थापना-त्रुटियों – juanefren

उत्तर

5

आप अपने रूप में एक कस्टम स्वच्छ समारोह है कि जांच करता है की जरूरत है सही ढंग से।

from django import forms 

class ContactForm(forms.Form): 
    # Everything as before. 
    ... 

    def clean_recipients(self): 
     data = self.cleaned_data['recipients'] 
     if "[email protected]" not in data: 
      raise forms.ValidationError("You have forgotten about Fred!") 

    # Always return the cleaned data, whether you have changed it or 
    # not. 
    return data 
0

यह docs

संक्षेप में से वास्तविक सिफारिश उदाहरण, cleaned_data वापस जाने के लिए याद है, और फार्म त्रुटियों बढ़ा है: