下面的代码行中出现了语法错误。我已经导入了数学,但是我的更新函数仍然不能工作。告诉我关键字不能是一个表达式,并引用最后3行。知道我做错什么了吗?
StoreLiquor.objects.filter(storeID=ID_Store, liquorID.BottleSize='750 ML', custom=False).update(StorePrice = liquorID.ShelfPrice)
StoreLiquor.objects.filter(storeID=ID_Store, liquorID.BottleSize='750 ML', custom=False).update(StorePrice = (float(liquorID.OffPremisePrice)) + (float(S750Increase)))
StoreLiquor.objects.filter(storeID=ID_Store, liquorID.BottleSize='750 ML', custom=False).update(StorePrice = (float(liquorID.OffPremisePrice) * (float(S750Increase)/100)) + float(liquorID.OffPremisePrice))发布于 2013-11-05 07:56:02
不能在参数名称中使用点,所以这个部分liquorID.BottleSize='750 ML'导致SyntaxError
若要在filter中使用相关模型,请使用跨越关系的查找
https://docs.djangoproject.com/en/dev/topics/db/queries/#lookups-that-span-relationships
Django提供了一种强大而直观的方法来“跟踪”查找中的关系,在后台为您自动处理SQL联接。要跨越关系,只需跨模型使用相关字段的字段名,以双下划线分隔,直到到达所需字段为止。
所以你的声明应该是这样的:
StoreLiquor.objects.filter(storeID=ID_Store,
liquorID__BottleSize='750 ML',
custom=False).update(StorePrice=liquorID__ShelfPrice)发布于 2013-11-05 07:48:34
我觉得应该是这样的
StoreLiquor.objects.filter(storeID=ID_Store, liquorID__BottleSize='750 ML', custom=False).update(StorePrice = liquorID__ShelfPrice)发布于 2013-11-05 07:49:01
您不能使用liquorID.BottleSize,它是无效的。只能使用有效的变量名。:
>>> def func():pass
>>> func(a.x=1)
File "<ipython-input-22-c75a0f520ac0>", line 1
SyntaxError: keyword can't be an expression使用liquorID__BottleSize代替。
相关:Why django has to use double underscore when making filter queries?
https://stackoverflow.com/questions/19783998
复制相似问题