Added get_proxy_object helper to filefields (#391)
This commit is contained in:
parent
1785ced655
commit
d9f538170b
@ -4,6 +4,7 @@ Changelog
|
||||
|
||||
Changes in 0.8.3
|
||||
================
|
||||
- Added get_proxy_object helper to filefields (#391)
|
||||
- Added QuerySetNoCache and QuerySet.no_cache() for lower memory consumption (#365)
|
||||
- Fixed sum and average mapreduce dot notation support (#375, #376, #393)
|
||||
- Fixed as_pymongo to return the id (#386)
|
||||
|
@ -1190,7 +1190,7 @@ class FileField(BaseField):
|
||||
# Check if a file already exists for this model
|
||||
grid_file = instance._data.get(self.name)
|
||||
if not isinstance(grid_file, self.proxy_class):
|
||||
grid_file = self.get_proxy_obj(key=key, instance=instance)
|
||||
grid_file = self.get_proxy_obj(key=self.name, instance=instance)
|
||||
instance._data[self.name] = grid_file
|
||||
|
||||
if not grid_file.key:
|
||||
@ -1225,9 +1225,9 @@ class FileField(BaseField):
|
||||
if collection_name is None:
|
||||
collection_name = self.collection_name
|
||||
|
||||
return self.proxy_class(key=key, instance=instance,
|
||||
db_alias=db_alias,
|
||||
collection_name=collection_name)
|
||||
return self.proxy_class(key=key, instance=instance,
|
||||
db_alias=db_alias,
|
||||
collection_name=collection_name)
|
||||
|
||||
def to_mongo(self, value):
|
||||
# Store the GridFS file id in MongoDB
|
||||
@ -1261,10 +1261,8 @@ class ImageGridFsProxy(GridFSProxy):
|
||||
applying field properties (size, thumbnail_size)
|
||||
"""
|
||||
field = self.instance._fields[self.key]
|
||||
# if the field from the instance has an attribute field
|
||||
# we use that one and hope for the best. Usually only container
|
||||
# fields have a field attribute.
|
||||
if hasattr(field, 'field'):
|
||||
# Handle nested fields
|
||||
if hasattr(field, 'field') and isinstance(field.field, FileField):
|
||||
field = field.field
|
||||
|
||||
try:
|
||||
|
@ -455,5 +455,31 @@ class FileTest(unittest.TestCase):
|
||||
self.assertEqual(1, TestImage.objects(Q(image1=grid_id)
|
||||
or Q(image2=grid_id)).count())
|
||||
|
||||
def test_complex_field_filefield(self):
|
||||
"""Ensure you can add meta data to file"""
|
||||
|
||||
class Animal(Document):
|
||||
genus = StringField()
|
||||
family = StringField()
|
||||
photos = ListField(FileField())
|
||||
|
||||
Animal.drop_collection()
|
||||
marmot = Animal(genus='Marmota', family='Sciuridae')
|
||||
|
||||
marmot_photo = open(TEST_IMAGE_PATH, 'rb') # Retrieve a photo from disk
|
||||
|
||||
photos_field = marmot._fields['photos'].field
|
||||
new_proxy = photos_field.get_proxy_obj('photos', marmot)
|
||||
new_proxy.put(marmot_photo, content_type='image/jpeg', foo='bar')
|
||||
marmot_photo.close()
|
||||
|
||||
marmot.photos.append(new_proxy)
|
||||
marmot.save()
|
||||
|
||||
marmot = Animal.objects.get()
|
||||
self.assertEqual(marmot.photos[0].content_type, 'image/jpeg')
|
||||
self.assertEqual(marmot.photos[0].foo, 'bar')
|
||||
self.assertEqual(marmot.photos[0].get().length, 8313)
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
Loading…
x
Reference in New Issue
Block a user