2013-11-04 52 views
1

我在我的Django應用以下幾種形式:在線表單集Django的更新視圖

class SurveyAreaForm(ModelForm): 
    class Media(object): 
     js = formset_media_js 

    class Meta: 
     model = SurveyArea 
     exclude = ['survey', ] 
     widgets = { 
      'action_by': CustomDateInput(), 
     } 


AreaFormSet = inlineformset_factory(Survey, SurveyArea, form=SurveyAreaForm) 

class SurveyForm(ModelForm): 
    class Meta: 
     model = Survey 
     exclude = ['tech', 'operator', ] 
     widgets = { 
      'date': CustomDateInput(), 
      'client': FilteredJQMRadioSelectWithAdd(), 
      'assessorSignature': SignatureInput(attrs={'id': 'assessorSignature'}), 
     } 

    def __init__(self, *args, **kwargs): 
     super(SurveyForm, self).__init__(*args, **kwargs) 
     self.fields['client'].empty_label = None 

我使用https://pypi.python.org/pypi/django-formset-js/0.3.0提供JavaScript來添加其他形式。

每個Survey對象都可以有一個或多個與之相關的SurveyAreas,並且我想讓它們使用相同的形式進行編輯。但是,我遇到了渲染初始數據的問題。這是我的觀點:

class SurveyUpdateView(SurveyValidateMixin, UpdateViewWithTech): 
    model = Survey 
    form_class = SurveyForm 
    success_url="/forms/survey/updated/" 
    template_name="my_django_app/survey_update.html" 

    def get(self, request, *args, **kwargs): 
     """ 
     Handles GET requests and instantiates blank version of the form 
     and its inline formsets. 
     """ 
     self.object = self.get_object() 
     form_class = self.get_form_class() 
     form = self.get_form(form_class) 

     # Get areas 
     areas = SurveyArea.objects.filter(survey=self.object).order_by('name').values() 

     # Render form 
     area_form = AreaFormSet(initial=areas) 
     return self.render_to_response(
      self.get_context_data(form=form, 
           area_form = area_form)) 

    def post(self, request, *args, **kwargs): 
     """ 
     Handles POST requests, instantiating a form instance and its inline 
     formsets with the passed POST variables and them checking them for 
     validity. 
     """ 
     self.object = self.get_object() 
     form_class = self.get_form_class() 
     form = self.get_form(form_class) 
     area_form = AreaFormSet(self.request.POST) 
     if (form.is_valid() and area_form.is_valid()): 
      return self.form_valid(form, area_form) 
     else: 
      return self.form_invalid(form, area_form) 

我也有一個SurveyCreateView,工作正常,並且驗證使用SurveyValidateMixin這些視圖之間共享。 SurveyUpdateView也從UpdateViewWithTech繼承,它基本上只是限制用戶的查詢集,並自動設置一個代表用戶的字段。

我遇到的問題是渲染初始數據。在SurveyUpdateViewget()方法中,我提取當前與該調查相關的所有區域,並使用PDB,我已經能夠確認在我獲取與此調查有關的區域(在可變區域中) ,數據似乎是正確的。下面是它的外觀與8個項目:

[{'description': u'A', 'photo': u'', 'action_required': u'A', 'action_by': datetime.date(2013, 11, 14), 'survey_id': 12L, u'id': 21L, 'action_taken': True, 'name': u'A'}, {'description': u'A', 'photo': u'', 'action_required': u'A', 'action_by': datetime.date(2013, 11, 14), 'survey_id': 12L, u'id': 19L, 'action_taken': True, 'name': u'A'}, {'description': u'A', 'photo': u'', 'action_required': u'A', 'action_by': datetime.date(2013, 11, 14), 'survey_id': 12L, u'id': 29L, 'action_taken': True, 'name': u'A'}, {'description': u'A', 'photo': u'', 'action_required': u'A', 'action_by': datetime.date(2013, 11, 14), 'survey_id': 12L, u'id': 30L, 'action_taken': True, 'name': u'A'}, {'description': u'B', 'photo': u'', 'action_required': u'B', 'action_by': datetime.date(2013, 11, 14), 'survey_id': 12L, u'id': 22L, 'action_taken': True, 'name': u'B'}, {'description': u'B', 'photo': u'', 'action_required': u'B', 'action_by': datetime.date(2013, 11, 14), 'survey_id': 12L, u'id': 20L, 'action_taken': True, 'name': u'B'}, {'description': u'B', 'photo': u'', 'action_required': u'B', 'action_by': datetime.date(2013, 11, 14), 'survey_id': 12L, u'id': 31L, 'action_taken': True, 'name': u'B'}, {'description': u'C', 'photo': u'', 'action_required': u'C', 'action_by': datetime.date(2013, 11, 14), 'survey_id': 12L, u'id': 23L, 'action_taken': True, 'name': u'C'}, {'description': u'X', 'photo': u'', 'action_required': u'X', 'action_by': datetime.date(2013, 11, 14), 'survey_id': 12L, u'id': 24L, 'action_taken': True, 'name': u'X'}, {'description': u'Y', 'photo': u'', 'action_required': u'Y', 'action_by': datetime.date(2013, 11, 14), 'survey_id': 12L, u'id': 25L, 'action_taken': True, 'name': u'Y'}, {'description': u'Z', 'photo': u'', 'action_required': u'Z', 'action_by': datetime.date(2013, 11, 14), 'survey_id': 12L, u'id': 26L, 'action_taken': True, 'name': u'Z'}] 

然而,傳遞的數據通過作爲initial值當實例AreaFormSet不能正確渲染數據。如果在AreaFormSet的定義中,我設置了extra的值,我得到了渲染區域的數量(如果未定義,則顯示三個,我相信這是默認值extra)。我想看到的行爲是每個現有區域以其自己的形式呈現。

再次使用PDB,如果在設置area_form.as_table()後轉儲area_form的值,我只能獲取extra中設置的表單數量,所以問題似乎是通過初始數據傳遞的。

我正確通過initial的值嗎?我知道initial的值應該是字典列表,它看起來對我來說是正確的,但我沒有得到正確數量的渲染區域。

回答

1

你試過

area_form = AreaFormSet(instance=self.object) 

,而不是

area_form = AreaFormSet(initial=areas) 

+0

這會產生一個錯誤:''Survey'對象不支持索引'我推測,因爲'self.object'包含主機表單的數據,而不是內聯formset工廠。我很想知道這是否已經解決。 –