我想將 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
uj5u.com熱心網友回復:
您需要使用 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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/364051.html
標籤:姜戈 django 模型 django-shell
上一篇:Django如何超越創建日期
