我的下面的模型有一些不需要的字段。我正在尝试向访问该字段的模型添加一个setter函数,但它表示该属性不存在。即使数据库中没有值,模型也有属性,对吧??!在这里相当困惑......
from app import app
from flask.ext.mongoalchemy import MongoAlchemy
from flask import Flask
from bson.objectid import ObjectId
app.config['MONGOALCHEMY_DATABASE'] = 'test-db'
db = MongoAlchemy(app)
...other models...
class Collection(db.Document):
title = db.StringField()
description = db.StringField(required=False)
source_url = db.StringField(required=False)
product_ids = db.ListField(db.ObjectIdField(), required=False)
@property
def products(self):
return session.query(Product).filter({"mongo_id": {in_: self.product_ids}})
def add_product(self, product):
pid = ObjectId(product.mongo_id)
self.product_ids.append(pid)
print self.product_ids
和错误......
File "/Users/me/Dropbox/development/test/venv/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/me/Dropbox/development/test/app/views.py", line 121, in collection_add_product
collection.add_product(product)
File "/Users/me/Dropbox/development/test/app/models.py", line 67, in add_product
print self.product_ids
File "/Users/me/Dropbox/development/test/venv/lib/python2.7/site-packages/mongoalchemy/fields/base.py", line 252, in __get__
raise AttributeError(self._name)
AttributeError: product_ids