From 4451843a393d21c40e0f764d1c8b8cc1119fa5c7 Mon Sep 17 00:00:00 2001 From: Harry Marr Date: Sun, 28 Feb 2010 17:52:29 +0000 Subject: [PATCH] Added docs for QuerySet.only --- docs/conf.py | 2 +- docs/guide/querying.rst | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/docs/conf.py b/docs/conf.py index a40a25ff..4e73fc36 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -38,7 +38,7 @@ master_doc = 'index' # General information about the project. project = u'MongoEngine' -copyright = u'2009, Harry Marr' +copyright = u'2009-2010, Harry Marr' # The version info for the project you're documenting, acts as replacement for # |version| and |release|, also used in various other places throughout the diff --git a/docs/guide/querying.rst b/docs/guide/querying.rst index 9d6ec827..6b14b12e 100644 --- a/docs/guide/querying.rst +++ b/docs/guide/querying.rst @@ -216,6 +216,35 @@ would be generating "tag-clouds":: from operator import itemgetter top_tags = sorted(tag_freqs.items(), key=itemgetter(1), reverse=True)[:10] +Retrieving a subset of fields +============================= +Sometimes a subset of fields on a :class:`~mongoengine.Document` is required, +and for efficiency only these should be retrieved from the database. This issue +is especially important for MongoDB, as fields may often be extremely large +(e.g. a :class:`~mongoengine.ListField` of +:class:`~mongoengine.EmbeddedDocument`\ s, which represent the comments on a +blog post. To select only a subset of fields, use +:meth:`~mongoengine.queryset.QuerySet.only`, specifying the fields you want to +retrieve as its arguments. Note that if fields that are not downloaded are +accessed, their default value (or :attr:`None` if no default value is provided) +will be given:: + + >>> class Film(Document): + ... title = StringField() + ... year = IntField() + ... rating = IntField(default=3) + ... + >>> Film(title='The Shawshank Redemption', year=1994, rating=5).save() + >>> f = Film.objects.only('title').first() + >>> f.title + 'The Shawshank Redemption' + >>> f.year # None + >>> f.rating # default value + 3 + +If you later need the missing fields, just call +:meth:`~mongoengine.Document.reload` on your document. + Advanced queries ================ Sometimes calling a :class:`~mongoengine.queryset.QuerySet` object with keyword