我想在 django shell 中将价格提高 30%。
模型.py:
price = models.FloatField(null=True)
贝壳:
from product.models import Product
Product.objects.all().update(price=price*1.3)
错误:
NameError: name 'price' is not defined
我想在 django shell 中将价格提高 30%。
模型.py:
price = models.FloatField(null=True)
贝壳:
from product.models import Product
Product.objects.all().update(price=price*1.3)
错误:
NameError: name 'price' is not defined
您需要使用 F 表达式来引用数据库中的字段 https://docs.djangoproject.com/en/3.2/ref/models/expressions/#f-expressions
from django.db.models import F
from product.models import Product
Product.objects.all().update(price=F('price')*1.3)