Using couchdb.mapping and unable to set an attribute

304 views Asked by At

Hi everyone and couchdb pros,

I have a mapping setup like such:

class Product(BaseModel):
  name= mapping.TextField()
  returned = mapping.DictField(mapping.Mapping.build(
    reason = mapping.TextField(),
    put_back = mapping.BooleanField()
  ))

I have a function called getById that takes an id and returns a product via:

return Product.load(db, id)

I have a problem: When I load a particular product, and try to set the returned dictionary like such:

product = Product.getById("123")
product.returned
>>> {}
product.returned['reason'] = "Test"
product.returned
>>> {}

NOTE: I only recently modified the model to have the "returned" key. Old product objects which are obtained using getById, does not have the "returned" key in them. Would this make a difference? It shouldn't right, after all this is a document DB and it should be flexible right?

Why does product.returned still return me an empty dictionary after setting the "reason" attribute?

I did some debugging and realized that the product variable is of the form:

<Product u'f90173026e859c65cfa08496115bf7bf'@u'11-3ea1135ceb6361631cbb08471d15df1e' {u'name': u'Printer', u'subProducts': [], u'company': {u'id': u'HP', u'name': u'Hewlett-Packard'}, u'productType':.......>

What kind of result is this? Is it a view result or is it a document?

Any help is greatly appreciated thanks!

Thanks.

1

There are 1 answers

0
Mark On BEST ANSWER

I found the problem. Apparently, couchdb-python does not allow you to "create" new keys in documents that were created using a previous mapping model. You would have to manually add the keys you want to all the documents first before you are able to set values for them.