Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django SyntaxError: keyword can't be an expression

Tags:

python

django

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))
like image 367
RuSs Avatar asked Nov 05 '13 07:11

RuSs


2 Answers

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)
like image 65
oleg Avatar answered Nov 14 '22 04:11

oleg


I think it should be something like this

StoreLiquor.objects.filter(storeID=ID_Store, liquorID__BottleSize='750 ML', custom=False).update(StorePrice = liquorID__ShelfPrice)
like image 27
Surya Avatar answered Nov 14 '22 04:11

Surya