2014-11-04 67 views
0

這是我的forms.py中的一個類 我想要做的是: 此表單上有兩個字段 - admin_time和execution_time。我想在保存之前對數據進行比較。在保存之前處理forms.py django中的數據

if admin_time: 
    execution_time=admin_time 

這是我打算實現的。

class TaskForm(forms.ModelForm): 
keywords = (forms.CharField(
      help_text=_('Please use commas to separate your keywords.'), 
      required=False, 
      widget=forms.TextInput(attrs={'class': 'medium-field'}))) 
admin_time = forms.CharField(
      help_text=_('Enter If more than 60 minutes.'), 
      required=False, 
      widget=forms.TextInput(attrs={'class': 'fill-width'})) 

def __init__(self, *args, **kwargs): 
    if kwargs['instance']: 
     initial = kwargs.get('initial', {}) 
     initial['keywords'] = kwargs['instance'].keywords_list 
     kwargs['initial'] = initial 
    super(TaskForm, self).__init__(*args, **kwargs) 

def _process_keywords(self, creator): 
    if 'keywords' in self.changed_data: 
     kw = [k.strip() for k in self.cleaned_data['keywords'].split(',')] 
     self.instance.replace_keywords(kw, creator) 

def clean(self): 
    cleaned_data = super(TaskForm, self).clean() 
    start_date = cleaned_data.get('start_date') 
    end_date = cleaned_data.get('end_date') 
    if start_date and end_date: 
     if start_date >= end_date: 
      raise forms.ValidationError(_("'End date' must be after 'Start date'")) 
    return cleaned_data 

def save(self, creator, *args, **kwargs): 
    self.instance.creator = creator 
    super(TaskForm, self).save(*args, **kwargs) 
    if kwargs.get('commit', True): 
     self._process_keywords(creator) 
    return self.instance 

class Media: 
    css = { 
     'all': ('css/admin_ace.css',) 
    } 

class Meta: 
    model = Task 
    fields = ('name', 'short_description', 'execution_time', 'difficulty', 
       'priority', 'repeatable', 'team', 'project', 'type', 'start_date', 
       'end_date', 'why_this_matters', 'prerequisites', 'instructions', 
       'is_draft', 'is_invalid') 
    widgets = { 
     'name': forms.TextInput(attrs={'size': 100, 'class': 'fill-width'}), 
     'short_description': forms.TextInput(attrs={'size': 100, 'class': 'fill-width'}), 
     'instructions': AceWidget(mode='markdown', theme='textmate', width='800px', 
            height='300px', wordwrap=True, 
            attrs={'class': 'fill-width'}), 
     'start_date': CalendarInput, 
     'end_date': CalendarInput, 
     'why_this_matters': forms.Textarea(attrs={'rows': 2, 'class': 'fill-width'}), 
     'prerequisites': forms.Textarea(attrs={'rows': 4, 'class': 'fill-width'}), 
    } 

我想什麼是添加此功能:

def _execution_time(self): 
    cleaned_data = super(TaskForm, self).clean() 
    data = cleaned_data.get('execution_time') 
    admin_time = cleaned_data.get('admin_time') 
    if admin_time: 
     data=admin_time 
    return data 

但它不工作。想要這個幫助。

+0

「但是有什麼問題。」什麼,具體是錯的? – Celeo 2014-11-04 18:35:32

+0

你在哪裏調用執行時間?如果將execution_time邏輯移動到乾淨的方法會發生什麼? – dm03514 2014-11-04 18:35:33

+0

我的意思是:這個驗證似乎沒有發生在我的代碼上。即使admin_time存在,我在下一個detail.html頁面上可以看到的只是execution_time。 – 2014-11-04 18:37:40

回答

相關問題