File tree Expand file tree Collapse file tree 1 file changed +17
-2
lines changed
Expand file tree Collapse file tree 1 file changed +17
-2
lines changed Original file line number Diff line number Diff line change @@ -10,8 +10,9 @@ Writing
1010GridFS support comes in the form of the :class: `~mongoengine.fields.FileField ` field
1111object. This field acts as a file-like object and provides a couple of
1212different 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+
3752Streaming
3853---------
3954
You can’t perform that action at this time.
0 commit comments