0

我想在 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
4

1 回答 1

0

您需要使用 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)
于 2021-11-23T16:09:34.010 回答