浏览代码

Fixed #27514 -- Tested Model.__init__ excessive number of fields.

Adam Chainz 8 年之前
父节点
当前提交
cc1f6f26b6
共有 2 个文件被更改,包括 6 次插入2 次删除
  1. 1 2
      django/db/models/base.py
  2. 5 0
      tests/model_regress/tests.py

+ 1 - 2
django/db/models/base.py

@@ -472,8 +472,7 @@ class Model(six.with_metaclass(ModelBase)):
         # overrides it. It should be one or the other; don't duplicate the work
         # The reason for the kwargs check is that standard iterator passes in by
         # args, and instantiation for iteration is 33% faster.
-        args_len = len(args)
-        if args_len > len(self._meta.concrete_fields):
+        if len(args) > len(self._meta.concrete_fields):
             # Daft, but matches old exception sans the err msg.
             raise IndexError("Number of args exceeds number of fields")
 

+ 5 - 0
tests/model_regress/tests.py

@@ -17,6 +17,11 @@ from .models import (
 
 
 class ModelTests(TestCase):
+    def test_model_init_too_many_args(self):
+        msg = "Number of args exceeds number of fields"
+        with self.assertRaisesMessage(IndexError, msg):
+            Worker(1, 2, 3, 4)
+
     # The bug is that the following queries would raise:
     # "TypeError: Related Field has invalid lookup: gte"
     def test_related_gte_lookup(self):