flask - python-couchdb add ViewField to Model after instantiation -
i trying build base model class used couch-views on (mainly crud).
i cannot add viewfields base class, since js string inside must changed per model class name.
 no problem inside base class __init__ reason, viewfield wont work.
when using viewfield so:
class mymodel(document):     only_carrots = viewfield("mymodel", js-string)   then if run:
mod = mymodel() mod.only_carrots    it show:
 <viewdefinition '_design/mymodel/_view/only_carrots'>
but if viewfield added during __init__, looks this:
<flaskext.couchdb.viewfield object @ 0x10fe8f190>   the code running on base model this:
    attr_name in dir(self):         if not attr_name.startswith("_") , attr_name not in ["id", "rev"]:             attr_val = getattr(self, attr_name)             if isinstance(attr_val, couchview):                 vd = viewfield(self.doc_type, attr_val.template.render(self.__dict__), name=attr_name, wrapper=self.__class__)                 setattr(self, attr_name, vd)   the couchview class own. used store info viewfield in way not detected code inside metaclasses.
the document class (of base model sub class) has __metaclass__. takes care of, @ least, part of work viewfield working, think have part covered in own class.  
the source python-couchdb found here:
 https://code.google.com/p/couchdb-python/source/browse/#hg%2fcouchdb
and flask-couchdb:
 https://bitbucket.org/leafstorm/flask-couchdb/src
so, how make viewfield work when added __init__ , therefore not available __new__ inside metaclasses?
many help.
well, think figured out. or @ least, workaround.
all viewfield fire viewdefinition wrapper-param filled class bound to.
so, when doing @ init time, call viewdefinition instead, so:
    def __init__(self, *args, **kwargs):     if not self.doc_type:         self.doc_type = self.__class__.__name__     attr_name in dir(self):         if not attr_name.startswith("_") , attr_name not in ["id", "rev"]:             attr_val = getattr(self, attr_name)             if isinstance(attr_val, couchview):                 setattr(self, attr_name, viewdefinition(self.doc_type, attr_name, attr_val.template.render(self.__dict__), wrapper=self))   of course, must remember instantiate model classes before adding them manager (it comes flask extension):
user = user() manager.add_document(user)   this bit me while testing. gotta stop doing when i'm tired.
 , reason need add _data = {} custom base class. can't figure out why not getting set properly, easy fix.
Comments
Post a Comment