Skip to content

Commit 4371057

Browse files
committed
Change SimpleArrayField size for length
1 parent dff26ac commit 4371057

File tree

5 files changed

+27
-27
lines changed

5 files changed

+27
-27
lines changed

django_mongodb_backend/fields/array.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ def formfield(self, **kwargs):
227227
"form_class": SimpleArrayField,
228228
"base_field": self.base_field.formfield(),
229229
"max_length": self.max_size,
230-
"size": self.size,
230+
"length": self.size,
231231
**kwargs,
232232
}
233233
)

django_mongodb_backend/forms/fields/array.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ class SimpleArrayField(forms.CharField):
1515
}
1616

1717
def __init__(
18-
self, base_field, *, delimiter=",", max_length=None, min_length=None, size=None, **kwargs
18+
self, base_field, *, delimiter=",", max_length=None, min_length=None, length=None, **kwargs
1919
):
2020
self.base_field = base_field
2121
self.delimiter = delimiter
2222
super().__init__(**kwargs)
23-
if (min_length is not None or max_length is not None) and size is not None:
23+
if (min_length is not None or max_length is not None) and length is not None:
2424
raise ImproperlyConfigured(
25-
"SimpleArrayField param 'size' cannot be "
25+
"SimpleArrayField param 'length' cannot be "
2626
"specified with 'max_length' or 'min_length'."
2727
)
2828
if min_length is not None:
@@ -31,9 +31,9 @@ def __init__(
3131
if max_length is not None:
3232
self.max_length = max_length
3333
self.validators.append(ArrayMaxLengthValidator(int(max_length)))
34-
if size is not None:
35-
self.size = size
36-
self.validators.append(LengthValidator(int(size)))
34+
if length is not None:
35+
self.length = length
36+
self.validators.append(LengthValidator(int(length)))
3737

3838
def clean(self, value):
3939
value = super().clean(value)

docs/source/ref/forms.rst

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Stores an :class:`~bson.objectid.ObjectId`.
3333
``SimpleArrayField``
3434
--------------------
3535

36-
.. class:: SimpleArrayField(base_field, delimiter=',', max_length=None, min_length=None)
36+
.. class:: SimpleArrayField(base_field, delimiter=',', length=None, max_length=None, min_length=None)
3737

3838
A field which maps to an array. It is represented by an HTML ``<input>``.
3939

@@ -91,6 +91,17 @@ Stores an :class:`~bson.objectid.ObjectId`.
9191
in cases where the delimiter is a valid character in the underlying
9292
field. The delimiter does not need to be only one character.
9393

94+
.. attribute:: length
95+
96+
This is an optional argument which validates that the array reaches at
97+
exactly the stated length.
98+
99+
.. note::
100+
Defining ``length`` along with ``max_length`` or ``min_length``
101+
will raise an exception. Use ``length`` for fixed-length arrays
102+
and ``max_length`` / ``min_length`` for variable-length arrays
103+
with an upper or lower limit.
104+
94105
.. attribute:: max_length
95106

96107
This is an optional argument which validates that the array does not
@@ -101,15 +112,6 @@ Stores an :class:`~bson.objectid.ObjectId`.
101112
This is an optional argument which validates that the array reaches at
102113
least the stated length.
103114

104-
.. attribute:: size
105-
106-
This is an optional argument which validates that the array reaches at
107-
exactly the stated length.
108-
109-
.. note::
110-
Defining ``size`` along with ``max_length`` or ``min_length`` will raise an exception.
111-
Use ``size`` for fixed-length arrays and ``max_length`` / ``min_length`` for variable-length arrays with an upper or lower limit.
112-
113115
.. admonition:: User friendly forms
114116

115117
``SimpleArrayField`` is not particularly user friendly in most cases,

docs/source/ref/models/fields.rst

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,6 @@ Some MongoDB-specific fields are available in ``django_mongodb_backend.fields``.
5555
),
5656
size=8,
5757
)
58-
active_pieces = ArrayField(
59-
models.CharField(max_length=10, blank=True),
60-
max_size=32
61-
)
6258

6359
Transformation of values between the database and the model, validation
6460
of data and configuration, and serialization are all delegated to the

tests/forms_tests_/test_array.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def test_min_length_singular(self):
116116
field.clean([1])
117117

118118
def test_size_length(self):
119-
field = SimpleArrayField(forms.CharField(max_length=27), size=4)
119+
field = SimpleArrayField(forms.CharField(max_length=27), length=4)
120120
with self.assertRaises(exceptions.ValidationError) as cm:
121121
field.clean(["a", "b", "c"])
122122
self.assertEqual(
@@ -131,7 +131,7 @@ def test_size_length(self):
131131
)
132132

133133
def test_size_length_singular(self):
134-
field = SimpleArrayField(forms.CharField(max_length=27), size=4)
134+
field = SimpleArrayField(forms.CharField(max_length=27), length=4)
135135
with self.assertRaises(exceptions.ValidationError) as cm:
136136
field.clean(["a"])
137137
self.assertEqual(
@@ -146,12 +146,14 @@ def test_required(self):
146146
self.assertEqual(cm.exception.messages[0], "This field is required.")
147147

148148
def test_misconfigured(self):
149-
msg = "SimpleArrayField param 'size' cannot be specified with 'max_length' or 'min_length'."
149+
msg = (
150+
"SimpleArrayField param 'length' cannot be specified with 'max_length' or 'min_length'."
151+
)
150152
with self.assertRaises(exceptions.ImproperlyConfigured) as cm:
151-
SimpleArrayField(forms.CharField(), max_length=3, size=2)
153+
SimpleArrayField(forms.CharField(), max_length=3, length=2)
152154
self.assertEqual(cm.exception.args[0], msg)
153155
with self.assertRaises(exceptions.ImproperlyConfigured) as cm:
154-
SimpleArrayField(forms.CharField(), min_length=3, size=2)
156+
SimpleArrayField(forms.CharField(), min_length=3, length=2)
155157
self.assertEqual(cm.exception.args[0], msg)
156158

157159
def test_model_field_formfield(self):
@@ -171,7 +173,7 @@ def test_model_field_formfield_size(self):
171173
model_field = ArrayField(models.CharField(max_length=27), size=4)
172174
form_field = model_field.formfield()
173175
self.assertIsInstance(form_field, SimpleArrayField)
174-
self.assertEqual(form_field.size, 4)
176+
self.assertEqual(form_field.length, 4)
175177

176178
def test_model_field_choices(self):
177179
model_field = ArrayField(models.IntegerField(choices=((1, "A"), (2, "B"))))

0 commit comments

Comments
 (0)