最も使用されているソファビュー (主に CRUD 用) を備えた基本モデルクラスを構築しようとしています。
モデルクラス名ごとに内部のjs文字列を少し変更する必要があるため、ViewFieldsを基本クラスに追加することはできません。
これは基本クラス内では問題ありません__init__
が、何らかの理由で ViewField が機能しません。
ViewField を通常のように使用する場合:
class MyModel(Document):
only_carrots = ViewField("mymodel", js-string)
次に、実行すると:
mod = MyModel()
mod.only_carrots
次のように表示されます。
<ViewDefinition '_design/mymodel/_view/only_carrots'>
ただし、ViewField が の実行中に追加されると__init__
、次のようになります。
<flaskext.couchdb.ViewField object at 0x10fe8f190>
基本モデルで実行されるコードは次のとおりです。
for attr_name in dir(self):
if not attr_name.startswith("_") and 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)
CouchView クラスは私自身のものです。メタクラス内のコードによって検出されない方法で ViewField の情報を格納するためにのみ使用されます。
Document クラス (私の基本モデルはサブクラスです) には__metaclass__
. これにより、少なくとも ViewField を機能させるための作業の一部が処理されますが、その部分は自分のクラスでカバーされていると思います。
python-couchdb のソースは、
https ://code.google.com/p/couchdb-python/source/browse/#hg%2Fcouchdb にあります。
Flask-CouchDB の場合:
https://bitbucket.org/leafstorm/flask-couchdb/src
では、ViewField が追加されたためにメタクラス内で__init__
使用できない場合、ViewField を機能させるにはどうすればよいでしょうか。__new__
助けてくれてありがとう。