1

I've got the below model with some fields that are not required. I'm trying to add a setter function to the model that accesses that field but it says the attribute doesn't exist. Even if there's no value in the DB, the model has the attribute, right??!? Quite confused here...

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

and the error...

  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
Scott
  • 3,204
  • 3
  • 31
  • 41
  • Sorry, yes. I was trying a bunch of things when I posted this. Thanks for catching that. Updated. – Scott Dec 20 '14 at 18:05
  • Since I'm so early in my journey with Mongo/Flask, I'm going to pass on MongoAlchemy. Using an ORM and a schema with Mongo seems counterintuitive to the reasons one my choose Mongo to begin with. MongoAlchemy could also use some more documentation. – Scott Dec 20 '14 at 22:36

0 Answers0