Array Reference field
ArrayField stores embedded models within a MongoDB array as embedded documents for each entry. However, if entries contain duplicate embedded documents, using the
ArrayField would result in unnecessary duplication and increased disk space usage. On the other hand, the Django
ManyToManyField only refers to a different table of entries. In addition however, it creates an intermediate “through/join” table which records all the mappings.
ArrayReferenceField is one of the most powerful features of Djongo. The
ArrayReferenceField is a bargain between the
ManyToManyField. Similar to the
ManyToManyField a separate collection is used for storing duplicate entries (instead of embedding them as an array). This means there is no data duplication. However, the intermediate “through/join” mapping table is completely skipped! This is achieved by storing only a reference to the entries in the embedded array.
ManyToManyField required two queries to fetch data, the
ArrayReferenceField requires just one query and is much faster. If you have used the
ManyToManyField, then you know how to use the
ArrayReferenceField. In fact, it implements the exact same API as the
ManyToManyField. You can replace all existing
ArrayReferenceField and everything will continue to work as is.
In the example the
Entry Model can be rewritten as follows:
name = models.CharField(max_length=200)
email = models.EmailField()
blog = models.EmbeddedField(
meta_data = models.EmbeddedField(
headline = models.CharField(max_length=255)
body_text = models.TextField()
authors = models.ArrayReferenceField(
n_comments = models.IntegerField()
Notice how the
Author model is no longer set as
abstract. This means a separate
author collection will be created in the DB. Simply set the
authors to a list containing several author instances. When the entry gets saved, only a reference to the primary_key of the author model is saved in the array. Upon retrieving an entry from the DB the corresponding authors are automatically looked up and the author list is populated.
ArrayReferenceField behaves exactly like the
ManyToManyField. However, underneath only references to the entries are being stored in the array.
def __init__(self, *args, **kwargs):
Same as the
ForeignKey Base class