3
votes

I have the following code and i'm trying to update an embedded document in a listfield.

store = store_service.get_store_from_product_id(product_id)
got_product, idx = get_product_from_store(store, product_id)

product = Product()
product.pid = got_product.pid
product.display_name = display_name
product.description = description
product.rank = rank
product.price = price
product.categories = categories
product.properties = properties

store.catalog.products[idx] = product

print store.catalog.products[idx].__unicode__()

store.save()

When I print out my product, it has the correct values, but when I save it, its not persisting. There are no errors being thrown. Any thoughts one what I could be doing wrong?

1

1 Answers

1
votes

store.catalog.products[idx] = product can be applied for DictField(). For ListField(). You can try:

store.catalog.products = [product]

or

store.catalog.products.append(product)

And you need to call save on the object:

store.save()

There is the possibility of atomic updates which can help in other cases:

Store.objects(id='123400000').update_one(push__catalog__products=product)