Skip to content

Commit 41f1843

Browse files
committed
Add docs
1 parent 4ebd62f commit 41f1843

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed

docs/source/ref/models/fields.rst

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,120 @@ These indexes use 0-based indexing.
295295
As described above for :class:`EmbeddedModelField`,
296296
:djadmin:`makemigrations` does not yet detect changes to embedded models.
297297

298+
Querying ``EmbeddedModelArrayField``
299+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
300+
301+
There are a number of custom lookups and a transform for :class:`EmbeddedModelArrayField`, similar to those available for :class:`ArrayField`.
302+
We will use the following example model::
303+
304+
from django.db import models
305+
from django_mongodb_backend.fields import EmbeddedModelArrayField, EmbeddedModelField,
306+
307+
class Tag(EmbeddedModel):
308+
label = models.CharField(max_length=100)
309+
310+
class Post(models.Model):
311+
name = models.CharField(max_length=200)
312+
tags = EmbeddedModelArrayField(Tag)
313+
314+
def __str__(self):
315+
return self.name
316+
317+
.. fieldlookup:: embeddedmodelarrayfield.overlap
318+
319+
``overlap``
320+
^^^^^^^^^^^
321+
322+
Returns objects where any of the embedded documents in the field match any of the values passed. For example:
323+
324+
.. code-block:: pycon
325+
326+
>>> Post.objects.create(
327+
... name="First post", tags=[Tag(label="thoughts"), Tag(label="django")]
328+
... )
329+
>>> Post.objects.create(name="Second post", tags=[Tag(label="thoughts")])
330+
>>> Post.objects.create(
331+
... name="Third post", tags=[Tag(label="tutorial"), Tag(label="django")]
332+
... )
333+
334+
>>> Post.objects.filter(tags__label__overlap=["thoughts"])
335+
<QuerySet [<Post: First post>, <Post: Second post>]>
336+
337+
>>> Post.objects.filter(tags__label__overlap=["tutorial", "thoughts"])
338+
<QuerySet [<Post: First post>, <Post: Second post>, <Post: Third post>]>
339+
340+
.. fieldlookup:: embeddedmodelarrayfield.len
341+
342+
``len``
343+
^^^^^^^
344+
345+
Returns the length of the embedded model array. The lookups available afterward are those available for :class:`~django.db.models.IntegerField`. For example:
346+
347+
.. code-block:: pycon
348+
349+
>>> Post.objects.create(
350+
... name="First post", tags=[Tag(label="thoughts"), Tag(label="django")]
351+
... )
352+
>>> Post.objects.create(name="Second post", tags=[Tag(label="thoughts")])
353+
354+
>>> Post.objects.filter(tags__len=1)
355+
<QuerySet [<Post: Second post>]>
356+
357+
.. fieldlookup:: embeddedmodelarrayfield.exact
358+
359+
``exact``
360+
^^^^^^^^^
361+
362+
Returns objects where **any** embedded model in the array exactly matches the given value. This acts like an existence filter on matching embedded documents.
363+
364+
.. code-block:: pycon
365+
366+
>>> Post.objects.create(
367+
... name="First post", tags=[Tag(label="thoughts"), Tag(label="django")]
368+
... )
369+
>>> Post.objects.create(name="Second post", tags=[Tag(label="tutorial")])
370+
371+
>>> Post.objects.filter(tags__label__exact="tutorial")
372+
<QuerySet [<Post: Second post>]>
373+
374+
Note that this does **not** require the whole array to match, only that at least one embedded document matches exactly.
375+
376+
Keytransforms
377+
^^^^^^^^^^^^^
378+
379+
Key transforms for \:class:`EmbeddedModelArrayField` allow querying fields of the embedded model. The transform checks if **any** element in the array has a field matching the condition, similar to MongoDB behavior. For example:
380+
381+
.. code-block:: pycon
382+
383+
>>> Post.objects.create(
384+
... name="First post", tags=[Tag(label="thoughts"), Tag(label="django")]
385+
... )
386+
>>> Post.objects.create(name="Second post", tags=[Tag(label="thoughts")])
387+
>>> Post.objects.create(
388+
... name="Third post",
389+
... tags=[Tag(label="django"), Tag(label="python"), Tag(label="thoughts")],
390+
... )
391+
392+
>>> Post.objects.filter(tags__label="django")
393+
<QuerySet [<Post: First post>, <Post: Third post>]>
394+
395+
396+
Transforms can be chained:
397+
398+
.. code-block:: pycon
399+
400+
>>> Post.objects.filter(tags__label__icontains="djan")
401+
<QuerySet [<Post: First post>, <Post: Third post>]>
402+
403+
404+
Indexed access is also supported:
405+
406+
.. code-block:: pycon
407+
408+
>>> Post.objects.filter(tags__0__label="django")
409+
<QuerySet [<Post: First post>]>
410+
411+
298412
``ObjectIdAutoField``
299413
---------------------
300414

docs/source/releases/5.2.x.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ New features
1212

1313
- Added :class:`~.fields.EmbeddedModelArrayField` for storing data as an array
1414
of model instances.
15+
- Added support for ``exact``, ``overlap``, and ``len`` lookups on
16+
:class:`~.fields.EmbeddedModelArrayField`.
1517

1618
Bug fixes
1719
---------

0 commit comments

Comments
 (0)