TAGS :Viewed: 7 - Published at: a few seconds ago

[ django SyntaxError: keyword can't be an expression ]

I'm getting a syntax error in the following lines of code. I've imported math, but my update function still won't work. Telling me keyword can't be an expression and cites the bottom 3 lines. Any idea what I'm doing wrong?

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))

Answer 1


You can't use dot in arguments names so this part liquorID.BottleSize='750 ML' causes SyntaxError

to use related models inside filter use lookups that span relationships

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.

So your statement should looks like this:

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

Answer 2


I think it should be something like this

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

Answer 3


You can't use liquorID.BottleSize, it is invalid. You can only use valid variable names.:

>>> def func():pass
>>> func(a.x=1)
  File "<ipython-input-22-c75a0f520ac0>", line 1
SyntaxError: keyword can't be an expression

Use liquorID__BottleSize instead.

Related: Why django has to use double underscore when making filter queries?

Answer 4


I think it's because of the name of your named arguments: you can't use dots in them so python has a problem with your liquorID.BottleSize named argument