Added docs for QuerySet.only

This commit is contained in:
Harry Marr 2010-02-28 17:52:29 +00:00
parent 5e2c5fa97b
commit 4451843a39
2 changed files with 30 additions and 1 deletions

View File

@ -38,7 +38,7 @@ master_doc = 'index'
# General information about the project. # General information about the project.
project = u'MongoEngine' 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 # The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the # |version| and |release|, also used in various other places throughout the

View File

@ -216,6 +216,35 @@ would be generating "tag-clouds"::
from operator import itemgetter from operator import itemgetter
top_tags = sorted(tag_freqs.items(), key=itemgetter(1), reverse=True)[:10] 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 Advanced queries
================ ================
Sometimes calling a :class:`~mongoengine.queryset.QuerySet` object with keyword Sometimes calling a :class:`~mongoengine.queryset.QuerySet` object with keyword