Skip to content

Commit a345548

Browse files
committed
Rename ArrayField.size to max_size
1 parent def97f0 commit a345548

File tree

6 files changed

+28
-26
lines changed

6 files changed

+28
-26
lines changed

django_mongodb_backend/fields/array.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ class ArrayField(CheckFieldDefaultMixin, Field):
2727
}
2828
_default_hint = ("list", "[]")
2929

30-
def __init__(self, base_field, size=None, **kwargs):
30+
def __init__(self, base_field, max_size=None, **kwargs):
3131
self.base_field = base_field
32-
self.size = size
33-
if self.size:
32+
self.max_size = max_size
33+
if self.max_size:
3434
self.default_validators = [
3535
*self.default_validators,
36-
ArrayMaxLengthValidator(self.size),
36+
ArrayMaxLengthValidator(self.max_size),
3737
]
3838
# For performance, only add a from_db_value() method if the base field
3939
# implements it.
@@ -125,8 +125,8 @@ def deconstruct(self):
125125
if path == "django_mongodb_backend.fields.array.ArrayField":
126126
path = "django_mongodb_backend.fields.ArrayField"
127127
kwargs["base_field"] = self.base_field.clone()
128-
if self.size is not None:
129-
kwargs["size"] = self.size
128+
if self.max_size is not None:
129+
kwargs["max_size"] = self.max_size
130130
return name, path, args, kwargs
131131

132132
def to_python(self, value):
@@ -210,7 +210,7 @@ def formfield(self, **kwargs):
210210
**{
211211
"form_class": SimpleArrayField,
212212
"base_field": self.base_field.formfield(),
213-
"max_length": self.size,
213+
"max_length": self.max_size,
214214
**kwargs,
215215
}
216216
)

docs/source/ref/models/fields.rst

+9-9
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ Some MongoDB-specific fields are available in ``django_mongodb_backend.fields``.
88
``ArrayField``
99
--------------
1010

11-
.. class:: ArrayField(base_field, size=None, **options)
11+
.. class:: ArrayField(base_field, max_size=None, **options)
1212

1313
A field for storing lists of data. Most field types can be used, and you
1414
pass another field instance as the :attr:`base_field
15-
<ArrayField.base_field>`. You may also specify a :attr:`size
16-
<ArrayField.size>`. ``ArrayField`` can be nested to store multi-dimensional
17-
arrays.
15+
<ArrayField.base_field>`. You may also specify a :attr:`max_size
16+
<ArrayField.max_size>`. ``ArrayField`` can be nested to store
17+
multi-dimensional arrays.
1818

1919
If you give the field a :attr:`~django.db.models.Field.default`, ensure
2020
it's a callable such as ``list`` (for an empty default) or a callable that
@@ -50,16 +50,16 @@ Some MongoDB-specific fields are available in ``django_mongodb_backend.fields``.
5050
board = ArrayField(
5151
ArrayField(
5252
models.CharField(max_length=10, blank=True),
53-
size=8,
53+
max_size=8,
5454
),
55-
size=8,
55+
max_size=8,
5656
)
5757

5858
Transformation of values between the database and the model, validation
5959
of data and configuration, and serialization are all delegated to the
6060
underlying base field.
6161

62-
.. attribute:: size
62+
.. attribute:: max_size
6363

6464
This is an optional argument.
6565

@@ -168,8 +168,8 @@ Index transforms
168168
^^^^^^^^^^^^^^^^
169169

170170
Index transforms index into the array. Any non-negative integer can be used.
171-
There are no errors if it exceeds the :attr:`size <ArrayField.size>` of the
172-
array. The lookups available after the transform are those from the
171+
There are no errors if it exceeds the :attr:`max_size <ArrayField.max_size>` of
172+
the array. The lookups available after the transform are those from the
173173
:attr:`base_field <ArrayField.base_field>`. For example:
174174

175175
.. code-block:: pycon

docs/source/releases/5.1.x.rst

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ Django MongoDB Backend 5.1.x
77

88
*Unreleased*
99

10+
- Backward-incompatible: :class:`~django_mongodb_backend.fields.ArrayField`\'s
11+
``size`` argument is renamed to ``max_size``.
1012
- Added support for :doc:`database caching </topics/cache>`.
1113

1214
5.1.0 beta 1

tests/forms_tests_/test_array.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ def test_model_field_formfield(self):
128128
self.assertIsInstance(form_field.base_field, forms.CharField)
129129
self.assertEqual(form_field.base_field.max_length, 27)
130130

131-
def test_model_field_formfield_size(self):
132-
model_field = ArrayField(models.CharField(max_length=27), size=4)
131+
def test_model_field_formfield_max_size(self):
132+
model_field = ArrayField(models.CharField(max_length=27), max_size=4)
133133
form_field = model_field.formfield()
134134
self.assertIsInstance(form_field, SimpleArrayField)
135135
self.assertEqual(form_field.max_length, 4)

tests/model_fields_/array_index_migrations/0001_initial.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class Migration(migrations.Migration):
2222
(
2323
"char",
2424
django_mongodb_backend.fields.ArrayField(
25-
models.CharField(max_length=10), db_index=True, size=100
25+
models.CharField(max_length=10), db_index=True, max_size=100
2626
),
2727
),
2828
("char2", models.CharField(max_length=11, db_index=True)),

tests/model_fields_/test_arrayfield.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,11 @@ def test_deconstruct(self):
9090
self.assertEqual(type(new.base_field), type(field.base_field))
9191
self.assertIsNot(new.base_field, field.base_field)
9292

93-
def test_deconstruct_with_size(self):
94-
field = ArrayField(models.IntegerField(), size=3)
93+
def test_deconstruct_with_max_size(self):
94+
field = ArrayField(models.IntegerField(), max_size=3)
9595
name, path, args, kwargs = field.deconstruct()
9696
new = ArrayField(*args, **kwargs)
97-
self.assertEqual(new.size, field.size)
97+
self.assertEqual(new.max_size, field.max_size)
9898

9999
def test_deconstruct_args(self):
100100
field = ArrayField(models.CharField(max_length=20))
@@ -801,8 +801,8 @@ def test_blank_true(self):
801801
# This should not raise a validation error
802802
field.clean([1, None], None)
803803

804-
def test_with_size(self):
805-
field = ArrayField(models.IntegerField(), size=3)
804+
def test_with_max_size(self):
805+
field = ArrayField(models.IntegerField(), max_size=3)
806806
field.clean([1, 2, 3], None)
807807
with self.assertRaises(exceptions.ValidationError) as cm:
808808
field.clean([1, 2, 3, 4], None)
@@ -811,8 +811,8 @@ def test_with_size(self):
811811
"List contains 4 items, it should contain no more than 3.",
812812
)
813813

814-
def test_with_size_singular(self):
815-
field = ArrayField(models.IntegerField(), size=1)
814+
def test_with_max_size_singular(self):
815+
field = ArrayField(models.IntegerField(), max_size=1)
816816
field.clean([1], None)
817817
msg = "List contains 2 items, it should contain no more than 1."
818818
with self.assertRaisesMessage(exceptions.ValidationError, msg):

0 commit comments

Comments
 (0)