Django SyntaxError:关键字不能是表达式

2024-05-19 09:34:47 发布

您现在位置:Python中文网/ 问答频道 /正文

下面几行代码中有语法错误。我已经导入了数学,但是我的更新函数仍然无法工作。告诉我关键字不能是表达式,并引用最下面的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))

Tags: storeidfalseobjectscustomupdatefloatfilter
3条回答

不能使用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?

我想应该是这样的

StoreLiquor.objects.filter(storeID=ID_Store, liquorID__BottleSize='750 ML', custom=False).update(StorePrice = liquorID__ShelfPrice)

不能在参数名中使用点,因此这部分liquorID.BottleSize='750 ML'会导致SyntaxError

要在filter中使用相关模型,请使用跨越关系的查找

https://docs.djangoproject.com/en/dev/topics/db/queries/#lookups-that-span-relationships

Django offers a powerful and intuitive way to “follow” relationships in lookups, taking care of the SQL JOINs for you automatically, behind the scenes. To span a relationship, just use the field name of related fields across models, separated by double underscores, until you get to the field you want.

所以你的陈述应该是这样的:

StoreLiquor.objects.filter(storeID=ID_Store, 
                           liquorID__BottleSize='750 ML',
                           custom=False).update(StorePrice=liquorID__ShelfPrice)

相关问题 更多 >

    热门问题