python - MongoAlchemy ObjectIdField resolution -
if have model contains objectidfield model, (django) use property
create getter , reference transparent in usage. so:
class image(db.document): name_full = db.stringfield(required=true) name_thumb = db.stringfield(required=false) source = db.stringfield(required=false) class product(db.document): name = db.stringfield() description = db.stringfield(required=false) image_id = db.objectidfield(required=false) def _get_image(self): try: return db.query(image).filter(mongo_id=self.image_id)[0] except indexerror: return none image = property(_get_image)
though, in practice error when trying access template:
<img src="{{ url_for('static', filename='images/products/'+product.image.name_thumb) }}"> undefinederror: 'app.models.product object' has no attribute 'image'
am going right way?
answering own question here. seems mistake in function itself, not declaration of property. once used session
instead of db
started working. shortened property declaration use @property
instead.
from mongoalchemy.session import session session = session.connect('flask-test') .... @property def image(self): try: return session.query(image).filter({"mongo_id":self.image_id})[0] except indexerror: return none
Comments
Post a Comment