lanto03 / couchdb-python

Automatically exported from code.google.com/p/couchdb-python
Other
0 stars 0 forks source link

allow restricting type of DictField values? #106

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
from http://groups.google.com/group/couchdb-
python/browse_thread/thread/e912efc0625d95c3:

It looks like there is a bare DictField which can have arbitrary keys/values 
with no schema, or a DictField with a Schema that pins down a fixed set of 
attributes each with its own Field type.  What I'd like is something where 
all the values are the same Field type (like ListField), but arbitrary keys 
can be used. 

Maybe there is a way easier way to do this or a cleaner way to roll this 
into the DictField, but I implemented it as a separate field type called 
MappingField with an implementation analogous to ListField.

eg:

>>> from datetime import date 
>>> from couchdb.schema import * 
>>> class Rating(Schema): 
...     rating = IntegerField() 
...     timestamp = DateField() 
>>> class UserInfo(Document): 
...     ratings = MappingField(DictField(Rating)) 
>>> info = UserInfo() 
>>> info.ratings['baloney'] = {'rating': 2, 'timestamp': date(1999, 1, 2)} 
>>> info.ratings['ham'] = {'rating': 3, 'timestamp': date(2000, 3, 4)} 
>>> info.ratings['pepperoni'] = {'rating': 5, 'timestamp': date(2001, 5, 6)} 
>>> info.ratings['ham'].rating 
3 
>>> info.ratings['ham'].timestamp 
datetime.date(2000, 3, 4)

Original issue reported on code.google.com by jabron...@gmail.com on 11 Dec 2009 at 11:10

Attachments:

GoogleCodeExporter commented 8 years ago

Original comment by djc.ochtman on 14 Dec 2009 at 10:32

GoogleCodeExporter commented 8 years ago
This issue has been migrated to GitHub. Please continue discussion here:

https://github.com/djc/couchdb-python/issues/106

Original comment by djc.ochtman on 15 Jul 2014 at 7:17