Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
#88 Handle getting the default value for a field explicitly. Instead…
… of overloading the descriptor __get__ method.

 - The _get_ method should return values in python types, eg a date time is a datetime.datetime instance
 - The default values need to be in basic json types, eg [1,2,3] , {}, str, int which can be serialized to the doc
 - We don't need to get the default values in __get__ anymore because the defaults are set on initialisation of the mapping, or on wrap in a mapping.
  • Loading branch information
JonathanWylie committed Mar 16, 2016
commit 505cf343f6cdca06da69824cdec33b721b55a35f
9 changes: 7 additions & 2 deletions couchdb/mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ def __get__(self, instance, owner):
value = instance._data.get(self.name)
if value is not None:
value = self._to_python(value)
elif self.default is not None:
return value

def get_default(self):
value = None
if self.default is not None:
default = self.default
if callable(default):
default = default()
Expand Down Expand Up @@ -139,7 +143,8 @@ def __init__(self, **values):
if attrname in values:
setattr(self, attrname, values.pop(attrname))
else:
setattr(self, attrname, getattr(self, attrname))
field = getattr(self.__class__, attrname)
setattr(self, attrname, field.get_default())

def __iter__(self):
return iter(self._data)
Expand Down