For instance:
>>> class Doc(Document):
... structure = { 'name': unicode }
...
>>> doc = db.mycol.Doc()
>>> doc
{ 'name': None }
>>> doc.name = 'FooBar'
>>> doc.save()
>>> doc
{ 'name': 'foobar' }
Is there a way I can do this without having to change the structure or
type?
class Doc(Document):
def save(self, *args, **kwargs):
self.name = self.name.lower()
super(Doc, self).save(*args, **kwargs)
> --
> To unsubscribe, reply using "remove me" as the subject.
>
--
Peter Bengtsson,
work www.fry-it.com
home www.peterbe.com
hobby www.issuetrackerproduct.com
fun crosstips.org
Yep; I just remembered about this when I woke up this morning.
However, thinking on I think it might be useful to define "filters"
for setting values... maybe something like:
>>> class Doc(Document):
... structure = { 'name': unicode }
... filters = { 'name': self._set_name }
... def _set_name(self, val):
... return unicode(val.lower())
...
>>> d = Doc()
>>> d.name = 'FooBar'
>>> d
{ 'name': u'foobar' }
>>> d.save()
Could help with unit-testing when there are many attributes which need
to be "cleaned" or adjusted when being set. Just a thought.
See below for Phillip's suggestion.
I've been lacking something like this too.
Overriding the save method is quite pythonic and works but it's far from neat.
I often want to do little basic manipulations of values before setting
them. For example turning byte strings into unicode and stripping
whitespace.
Any thoughts on this?
--
Feel free to discuss this feature before implementation.