Skip to content

Commit fedce2b

Browse files
committed
Add docs
1 parent dc5154d commit fedce2b

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed

docs/source/ref/models/fields.rst

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

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

0 commit comments

Comments
 (0)