2016-12-30 99 views
0

我有一個formset,我想有一個有點任意數量的表單。這裏是formset類。即使有太多的方式驗證django formset

class ChartDatasetForm(forms.Form): 
    """Giving the form a dataset, meant to be repeated with formsets""" 

    data_label = forms.CharField(label="Data Label", max_length=100) 
    dataset = forms.CharField(label="Data", max_length=1000) 


ChartDatasetFormset = formset_factory(
    ChartDatasetForm, min_num=1, validate_min=True, max_num=1000, validate_max=True) 

,然後我有一些像這樣的測試...

class FormTests(TestCase): 
    """Testing that the forms work as expected""" 

    def setUp(self): 
     """Setting up data for forms""" 

     self.dataset = { 
      'form-TOTAL_FORMS': 2, 
      'form-INITIAL_FORMS': 0, 
      'form-MIN_NUM_FORMS': 1, 
      'form-MAX_NUM_FORMS': 1000, 
      'form-0-data_label': '', 
      'form-0-dataset': '', 
      'form-1-data_label': '', 
      'form-1-dataset': '', 
     } 

    def test_chart_dataset_validates_with_2_sets(self): 
     """Testing that the form validates when there are two datasets""" 
     for n in range(2): 
      self.dataset['form-%s-data_label' % n] = 'the labels' 
      self.dataset['form-%s-dataset' % n] = '1, 2' 
     form = ChartDatasetFormset(self.dataset) 
     self.assertTrue(form.is_valid()) 

    def test_chart_dataset_validates_with_3000_sets(self): 
     """Running up to the max amount of forms to see if it validates""" 
     for n in range(3000): 
      self.dataset['form-%s-data_label' % n] = 'the labels' 
      self.dataset['form-%s-dataset' % n] = '1, 2' 
     form = ChartDatasetFormset(self.dataset) 
     form.clean() 
     self.assertTrue(form.is_valid()) 

你可以看到,當有3000點的形式,即使我設置了max_num在1000爲什麼圖表驗證這個?

回答

2

嘗試改變

'form-TOTAL_FORMS': 2 

至> 1000,我想測試測試的此屬性,而不是實際的數據

'form-TOTAL_FORMS': 1003, 


Traceback (most recent call last): 
File "/home/bobby/Documents/code/python/hello/try/tests.py", line 38, in test_chart_dataset_validates_with_3000_sets 
self.assertTrue(form.is_valid()) 
AssertionError: False is not true