Skip to content

Commit 66a0fca

Browse files
author
erdenezul
authored
Merge pull request #2229 from bagerard/improve_doc_gridfs
improve doc of GridFS
2 parents b3dbb87 + e7c7a66 commit 66a0fca

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

docs/guide/gridfs.rst

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ Writing
1010
GridFS support comes in the form of the :class:`~mongoengine.fields.FileField` field
1111
object. This field acts as a file-like object and provides a couple of
1212
different ways of inserting and retrieving data. Arbitrary metadata such as
13-
content type can also be stored alongside the files. In the following example,
14-
a document is created to store details about animals, including a photo::
13+
content type can also be stored alongside the files. The object returned when accessing a
14+
FileField is a proxy to `Pymongo's GridFS <https://api.mongodb.com/python/current/examples/gridfs.html#gridfs-example>`_
15+
In the following example, a document is created to store details about animals, including a photo::
1516

1617
class Animal(Document):
1718
genus = StringField()
@@ -34,6 +35,20 @@ field. The file can also be retrieved just as easily::
3435
photo = marmot.photo.read()
3536
content_type = marmot.photo.content_type
3637

38+
.. note:: If you need to read() the content of a file multiple times, you'll need to "rewind"
39+
the file-like object using `seek`::
40+
41+
marmot = Animal.objects(genus='Marmota').first()
42+
content1 = marmot.photo.read()
43+
assert content1 != ""
44+
45+
content2 = marmot.photo.read() # will be empty
46+
assert content2 == ""
47+
48+
marmot.photo.seek(0) # rewind the file by setting the current position of the cursor in the file to 0
49+
content3 = marmot.photo.read()
50+
assert content3 == content1
51+
3752
Streaming
3853
---------
3954

0 commit comments

Comments
 (0)