Merge pull request #1848 from bagerard/patch-4

Update gridfs.rst - make it clear that user must call Document.save()
This commit is contained in:
erdenezul 2018-08-26 08:28:42 +08:00 committed by GitHub
commit cc00a321da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 8 additions and 6 deletions

View File

@ -53,7 +53,8 @@ Deletion
Deleting stored files is achieved with the :func:`delete` method:: Deleting stored files is achieved with the :func:`delete` method::
marmot.photo.delete() marmot.photo.delete() # Deletes the GridFS document
marmot.save() # Saves the GridFS reference (being None) contained in the marmot instance
.. warning:: .. warning::
@ -71,4 +72,5 @@ Files can be replaced with the :func:`replace` method. This works just like
the :func:`put` method so even metadata can (and should) be replaced:: the :func:`put` method so even metadata can (and should) be replaced::
another_marmot = open('another_marmot.png', 'rb') another_marmot = open('another_marmot.png', 'rb')
marmot.photo.replace(another_marmot, content_type='image/png') marmot.photo.replace(another_marmot, content_type='image/png') # Replaces the GridFS document
marmot.save() # Replaces the GridFS reference contained in marmot instance

View File

@ -1528,9 +1528,9 @@ class GridFSProxy(object):
return '<%s: %s>' % (self.__class__.__name__, self.grid_id) return '<%s: %s>' % (self.__class__.__name__, self.grid_id)
def __str__(self): def __str__(self):
name = getattr( gridout = self.get()
self.get(), 'filename', self.grid_id) if self.get() else '(no file)' filename = getattr(gridout, 'filename') if gridout else '<no file>'
return '<%s: %s>' % (self.__class__.__name__, name) return '<%s: %s (%s)>' % (self.__class__.__name__, filename, self.grid_id)
def __eq__(self, other): def __eq__(self, other):
if isinstance(other, GridFSProxy): if isinstance(other, GridFSProxy):

View File

@ -54,7 +54,7 @@ class FileTest(MongoDBTestCase):
result = PutFile.objects.first() result = PutFile.objects.first()
self.assertTrue(putfile == result) self.assertTrue(putfile == result)
self.assertEqual("%s" % result.the_file, "<GridFSProxy: hello>") self.assertEqual("%s" % result.the_file, "<GridFSProxy: hello (%s)>" % result.the_file.grid_id)
self.assertEqual(result.the_file.read(), text) self.assertEqual(result.the_file.read(), text)
self.assertEqual(result.the_file.content_type, content_type) self.assertEqual(result.the_file.content_type, content_type)
result.the_file.delete() # Remove file from GridFS result.the_file.delete() # Remove file from GridFS