Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename ArrayField.size to max_size #273

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions django_mongodb_backend/fields/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ class ArrayField(CheckFieldDefaultMixin, Field):
}
_default_hint = ("list", "[]")

def __init__(self, base_field, size=None, **kwargs):
def __init__(self, base_field, max_size=None, **kwargs):
self.base_field = base_field
self.size = size
if self.size:
self.max_size = max_size
if self.max_size:
self.default_validators = [
*self.default_validators,
ArrayMaxLengthValidator(self.size),
ArrayMaxLengthValidator(self.max_size),
]
# For performance, only add a from_db_value() method if the base field
# implements it.
Expand Down Expand Up @@ -124,12 +124,9 @@ def deconstruct(self):
name, path, args, kwargs = super().deconstruct()
if path == "django_mongodb_backend.fields.array.ArrayField":
path = "django_mongodb_backend.fields.ArrayField"
kwargs.update(
{
"base_field": self.base_field.clone(),
"size": self.size,
}
)
kwargs["base_field"] = self.base_field.clone()
if self.max_size is not None:
kwargs["max_size"] = self.max_size
return name, path, args, kwargs

def to_python(self, value):
Expand Down Expand Up @@ -213,7 +210,7 @@ def formfield(self, **kwargs):
**{
"form_class": SimpleArrayField,
"base_field": self.base_field.formfield(),
"max_length": self.size,
"max_length": self.max_size,
**kwargs,
}
)
Expand Down
20 changes: 10 additions & 10 deletions docs/source/ref/models/fields.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ Some MongoDB-specific fields are available in ``django_mongodb_backend.fields``.
``ArrayField``
--------------

.. class:: ArrayField(base_field, size=None, **options)
.. class:: ArrayField(base_field, max_size=None, **options)

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

If you give the field a :attr:`~django.db.models.Field.default`, ensure
it's a callable such as ``list`` (for an empty default) or a callable that
Expand Down Expand Up @@ -50,21 +50,21 @@ Some MongoDB-specific fields are available in ``django_mongodb_backend.fields``.
board = ArrayField(
ArrayField(
models.CharField(max_length=10, blank=True),
size=8,
max_size=8,
),
size=8,
max_size=8,
)

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

.. attribute:: size
.. attribute:: max_size

This is an optional argument.

If passed, the array will have a maximum size as specified, validated
only by forms.
by forms and model validation, but not enforced by the database.

Querying ``ArrayField``
~~~~~~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -168,8 +168,8 @@ Index transforms
^^^^^^^^^^^^^^^^

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

.. code-block:: pycon
Expand Down
2 changes: 2 additions & 0 deletions docs/source/releases/5.1.x.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Django MongoDB Backend 5.1.x

*Unreleased*

- Backward-incompatible: :class:`~django_mongodb_backend.fields.ArrayField`\'s
``size`` argument is renamed to ``max_size``.
- Added support for :doc:`database caching </topics/cache>`.
- Fixed ``QuerySet.raw_aggregate()`` field initialization when the document key
order doesn't match the order of the model's fields.
Expand Down
4 changes: 2 additions & 2 deletions tests/forms_tests_/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ def test_model_field_formfield(self):
self.assertIsInstance(form_field.base_field, forms.CharField)
self.assertEqual(form_field.base_field.max_length, 27)

def test_model_field_formfield_size(self):
model_field = ArrayField(models.CharField(max_length=27), size=4)
def test_model_field_formfield_max_size(self):
model_field = ArrayField(models.CharField(max_length=27), max_size=4)
form_field = model_field.formfield()
self.assertIsInstance(form_field, SimpleArrayField)
self.assertEqual(form_field.max_length, 4)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Migration(migrations.Migration):
),
(
"field",
django_mongodb_backend.fields.ArrayField(models.IntegerField(), size=None),
django_mongodb_backend.fields.ArrayField(models.IntegerField()),
),
],
options={},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ class Migration(migrations.Migration):
migrations.AddField(
model_name="integerarraydefaultmodel",
name="field_2",
field=django_mongodb_backend.fields.ArrayField(
models.IntegerField(), default=[], size=None
),
field=django_mongodb_backend.fields.ArrayField(models.IntegerField(), default=[]),
preserve_default=False,
),
]
2 changes: 1 addition & 1 deletion tests/model_fields_/array_index_migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Migration(migrations.Migration):
(
"char",
django_mongodb_backend.fields.ArrayField(
models.CharField(max_length=10), db_index=True, size=100
models.CharField(max_length=10), db_index=True, max_size=100
),
),
("char2", models.CharField(max_length=11, db_index=True)),
Expand Down
21 changes: 11 additions & 10 deletions tests/model_fields_/test_arrayfield.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,16 @@ class MyModel(models.Model):
def test_deconstruct(self):
field = ArrayField(models.IntegerField())
name, path, args, kwargs = field.deconstruct()
self.assertEqual(kwargs.keys(), {"base_field"})
new = ArrayField(*args, **kwargs)
self.assertEqual(type(new.base_field), type(field.base_field))
self.assertIsNot(new.base_field, field.base_field)

def test_deconstruct_with_size(self):
field = ArrayField(models.IntegerField(), size=3)
def test_deconstruct_with_max_size(self):
field = ArrayField(models.IntegerField(), max_size=3)
name, path, args, kwargs = field.deconstruct()
new = ArrayField(*args, **kwargs)
self.assertEqual(new.size, field.size)
self.assertEqual(new.max_size, field.max_size)

def test_deconstruct_args(self):
field = ArrayField(models.CharField(max_length=20))
Expand Down Expand Up @@ -722,7 +723,7 @@ class MigrationsTests(TransactionTestCase):
)
def test_adding_field_with_default(self):
class IntegerArrayDefaultModel(models.Model):
field = ArrayField(models.IntegerField(), size=None)
field = ArrayField(models.IntegerField())

table_name = "model_fields__integerarraydefaultmodel"
self.assertNotIn(table_name, connection.introspection.table_names(None))
Expand All @@ -734,8 +735,8 @@ class IntegerArrayDefaultModel(models.Model):
call_command("migrate", "model_fields_", "0002", verbosity=0)

class UpdatedIntegerArrayDefaultModel(models.Model):
field = ArrayField(models.IntegerField(), size=None)
field_2 = ArrayField(models.IntegerField(), default=[], size=None)
field = ArrayField(models.IntegerField())
field_2 = ArrayField(models.IntegerField(), default=[])

class Meta:
db_table = "model_fields__integerarraydefaultmodel"
Expand Down Expand Up @@ -800,8 +801,8 @@ def test_blank_true(self):
# This should not raise a validation error
field.clean([1, None], None)

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

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