使用update()时,MongoEngine十进制字段不会递减?

时间:2016-06-20 07:10:11

标签: django mongoengine

我试图通过使用mongoengine document中提到的减量来减少total_due_amount的值。但我得到InvalidDocument异常类型。 无法编码对象:十进制(' -10.00')

我有像

这样的模特
import mongoengine as me

class Orders(me.Document):
         order_id = me.SequenceField(primary_key=True)
         total_due_amount = me.DecimalField(required=True)
         # other  irrevelant fields 

我有像

这样的观点
def update_total_due_amount(request):
      # some irrelevant pre processing
      total_amount_paid = 10    # after some preprocessing assume total_amount_paid is 10
      order_obj_update = Orders.objects(order_id=order_id).update_one(dec__total_due_amount=total_amount_paid) # this raises an exception

我已经通过

解决了这个问题
Orders.objects(order_id=order_id).update_one(dec__total_due_amount=int(total_amount_paid)) # observe the int()

这是应该怎么做的?或者我错过了什么? Stack Trace如下:

InvalidDocument at /cart/place_order
Cannot encode object: Decimal('-210.00')
Request Method: POST
Request URL:    http://127.0.0.1:8000/cart/place_order
Django Version: 1.9.4
Exception Type: InvalidDocument
Exception Value:    
Cannot encode object: Decimal('-210.00')
Exception Location: /Users/Shyam/Documents/EatFresh/Orders/VirtualEnv/lib/python2.7/site-packages/pymongo/pool.py in _raise_connection_failure, line 346
Python Executable:  /Users/Shyam/Documents/EatFresh/Orders/VirtualEnv/bin/python
Python Version: 2.7.10
Python Path:    
['/Users/Shyam/Documents/EatFresh/Orders',
 '/Users/Shyam/Documents/EatFresh/Orders/VirtualEnv/lib/python2.7/site-packages/efInternalAuth-0.1-py2.7.egg',
 '/Users/Shyam/Documents/EatFresh/Orders/VirtualEnv/lib/python2.7/site-packages/boto-2.40.0-py2.7.egg',
 '/Users/Shyam/Documents/EatFresh/Orders/VirtualEnv/lib/python27.zip',
 '/Users/Shyam/Documents/EatFresh/Orders/VirtualEnv/lib/python2.7',
 '/Users/Shyam/Documents/EatFresh/Orders/VirtualEnv/lib/python2.7/plat-darwin',
 '/Users/Shyam/Documents/EatFresh/Orders/VirtualEnv/lib/python2.7/plat-mac',
 '/Users/Shyam/Documents/EatFresh/Orders/VirtualEnv/lib/python2.7/plat-mac/lib-scriptpackages',
 '/Users/Shyam/Documents/EatFresh/Orders/VirtualEnv/Extras/lib/python',
 '/Users/Shyam/Documents/EatFresh/Orders/VirtualEnv/lib/python2.7/lib-tk',
 '/Users/Shyam/Documents/EatFresh/Orders/VirtualEnv/lib/python2.7/lib-old',
 '/Users/Shyam/Documents/EatFresh/Orders/VirtualEnv/lib/python2.7/lib-dynload',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
 '/Users/Shyam/Documents/EatFresh/Orders/VirtualEnv/lib/python2.7/site-packages']
Server time:    Mon, 20 Jun 2016 14:58:29 +0530

0 个答案:

没有答案
相关问题