django formset验证即使有太多的方法

时间:2016-12-30 11:13:19

标签: python django

我有一个formset,我希望有一些任意数量的表单。这是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())

如您所见,即使我将max_num设置为1000,图表也会在有3000个表单时进行验证。为什么会这样?

1 个答案:

答案 0 :(得分:2)

尝试更改

begin
  system("stty raw -echo")
  print (acc = "Edit me: ")
  loop.each_with_object(acc) do |_,acc|
    sym = $stdin.getc
    case sym.ord
    when 13    # carriage return
      break acc
    when 127   # backspace
      print "\e[1D \e[1D"
      acc.slice!(acc.length - 1) if acc.length > 0
    else       # regular symbol
      print sym
      acc << sym
    end
  end
ensure
  system("stty -raw echo")
  puts
  puts "\e[0mEntered: |#{acc}|"
end

到&gt; 1000,我认为针对此属性的测试测试而不是实际数据

'form-TOTAL_FORMS': 2