Add GridFSProxy.__nonzero__

For documents that do not have a value set for a given field, most field types
return None (or [] in the case of ListField). This makes it easy to test
whether a field has been set using "if doc.field". FileFields, on the other
hand, always return a GridFSProxy. Adding GridFSProxy.__nonzero__ which simply
checks for a grid_id allows the same boolean-test pattern for FileFields, as
well.
This commit is contained in:
Zak Johnson
2011-06-29 20:48:39 -07:00
parent 14be7ba2e2
commit 3d15a3b3e2
2 changed files with 19 additions and 1 deletions

View File

@@ -723,6 +723,9 @@ class GridFSProxy(object):
def __get__(self, instance, value):
return self
def __nonzero__(self):
return bool(self.grid_id)
def get(self, id=None):
if id:
self.grid_id = id
@@ -805,7 +808,7 @@ class FileField(BaseField):
# Check if a file already exists for this model
grid_file = instance._data.get(self.name)
self.grid_file = grid_file
if self.grid_file:
if isinstance(self.grid_file, GridFSProxy):
if not self.grid_file.key:
self.grid_file.key = self.name
self.grid_file.instance = instance