2012-11-09 37 views
2

我注意到我的Django應用程序中有一個奇怪的行爲,它運行在apache/mod_wsgi上。有一個屏幕顯示一個表格,基本上是一個下拉列表,其中列出了可用於安排給定網站的列表,根據給定的每週容量(3個網站/周)與已安排的網站總數之間的差異計算周。Django形成不刷新的類

def schedule(request, site_id): 
    site = Site.objects.get(pk=site_id) 
    if request.method == 'POST': 
     form = ScheduleForm(request.POST) 
     if form.is_valid(): 
      (year, week) = request.POST['available_slots'].split('/') 
      site.scheduled = week2date(int(year), int(week[1:])) 
      site.save() 
      return HttpResponseRedirect('/stats/') 
    else: 
     form = ScheduleForm() 

    return render_to_response('followup/schedule_form.html',{ 
      'form': form, 
      'site': site 
     }, context_instance=RequestContext(request)) 

這裏是形式類(隨訪/ forms.py):

class ScheduleForm(forms.Form): 
    """ 
    Temporary lists 
    """ 
    schedules = Site.objects.filter(
      scheduled__isnull=False 
     ).values('scheduled').annotate(Count('id')).order_by('scheduled') 
    integration = {} 
    available_integration = [] 

    # This aggregates all schedules by distinct weeks 
    for schedule in schedules: 
     if schedule['scheduled'].strftime('%Y/W%W') in integration.keys(): 
      integration[schedule['scheduled'].strftime('%Y/W%W')] += schedule['id__count'] 
     else: 
      integration[schedule['scheduled'].strftime('%Y/W%W')] = schedule['id__count'] 

    for w in range(12): # Calculates availability for the next 3 months (3months*4 weeks) 
     dt = (date.today() + timedelta(weeks=w)).strftime('%Y/W%W') 
     if dt in integration.keys(): 
      capacity = 3-integration[dt] 
     else: 
      capacity = 3 

     if capacity>0: 
      available_integration.append([dt, capacity]) 

    """ 
    Form 
    """ 
    available_slots = forms.ChoiceField(
     [[slot[0], '%s (%s slots available)' % (slot[0], slot[1])] for slot in available_integration] 
    ) 

class IntegrateForm(forms.Form): 
    integrated_on = forms.DateField(widget=AdminDateWidget()) 

這種形式(ScheduleForm)從下面的視圖(隨訪/ views.py)呈現實際上工作正常,但唯一的問題是,當網站計劃時,可用性列表不會刷新,除非我每次安排網站時重新啓動apache進程。

這就像如果可用性列表將緩存由窗體類...

任何想法將受到熱烈歡迎。提前感謝您提供任何幫助。

+0

塞巴斯蒂安我編輯你的問題,包括你的正確形式。如果您有時間,請刪除您的答案,因爲它應該是您原始問題中的「編輯」。 – rantanplan

+0

@Sebastien請參閱[這裏](http://meta.stackexchange.com/q/155173/155556)爲什麼你的答案沒有被刪除。 – Neal

回答

3

這和python的工作方式有關,而不是django。

下面

class ScheduleForm(forms.Form): 
    """ 
    Temporary lists 
    """ 
    schedules = Site.objects.filter(
      scheduled__isnull=False 
     ).values('scheduled').annotate(Count('id')).order_by('scheduled') 
    integration = {} 
    available_integration = [] 

該代碼將只有一次evalutated - 在服務器啓動時。

你不應該在class級別上做這些事情,而應該在instance級別上做這些事情。 最有可能在您的表單的__init__方法中。

看下面的例子:

+0

絕對正確......我已經修改了我的代碼,如下所示,現在它的功能就像一個魅力:) –

+0

@SebastienDamaye很高興知道! – rantanplan

2

我已經修改我的代碼如下,它現在的工作就像一個魅力:) 一世 在這裏提供它以防止出現類似問題的任何人。

class ScheduleForm(forms.Form): 
    available_slots = forms.ChoiceField() 

    def __init__(self, *args, **kwargs): 
     super(ScheduleForm, self).__init__(*args, **kwargs) 

     schedules = Site.objects.filter(
       scheduled__isnull=False 
      ).values('scheduled').annotate(Count('id')).order_by('scheduled') 
     integration = {} 
     available_integration = [] 

     # This aggregates all schedules by distinct weeks 
     for schedule in schedules: 
      if schedule['scheduled'].strftime('%Y/W%W') in integration.keys(): 
       integration[schedule['scheduled'].strftime('%Y/W%W')] += schedule['id__count'] 
      else: 
       integration[schedule['scheduled'].strftime('%Y/W%W')] = schedule['id__count'] 

     for w in range(12): # Calculates availability for the next 3 months (3months*4 weeks) 
      dt = (date.today() + timedelta(weeks=w)).strftime('%Y/W%W') 
      if dt in integration.keys(): 
       capacity = 3-integration[dt] 
      else: 
       capacity = 3 

      if capacity>0: 
       available_integration.append([dt, capacity]) 

     self.fields['available_slots'].choices = [[slot[0], '%s (%s slots available)' % (slot[0], slot[1])] for slot in available_integration]