我有这样一个产品表,其中包含prod_id,prod_name,prod_price ...
let data = $('form').serializeArray();
let answers = {};
data.forEach(function(element) {
// check if the element contains a answer input value
if(element.name.indexOf('answer') !== -1) {
answers[element.name] = element.value;
}
});
console.log($('form').serialize());
// output: answer%5B1%5D=1&answer%5B2%5D=2&answer%5B4%5D=3&answer%5B3%5D=4&sort=relevance&query=&area=..... etc
console.log(answers.serialize());
// output: is not a function.
产品价格可以通过以下方式更新:
MySQL [distributor]> select * from products;
+---------+---------+---------------------+------------+-----------------------------------------------------------------------+
| prod_id | vend_id | prod_name | prod_price | prod_desc |
+---------+---------+---------------------+------------+-----------------------------------------------------------------------+
| BNBG01 | DLL01 | Fish bean bag toy | 3.49 | Fish bean bag toy, complete with bean bag worms with which to feed it |
| BNBG02 | DLL01 | Bird bean bag toy | 3.49 | Bird bean bag toy, eggs are not included |
| BNBG03 | DLL01 | Rabbit bean bag toy | 3.49 | Rabbit bean bag toy, comes with bean bag carrots |
| BR01 | BRS01 | 8 inch teddy bear | 5.99 | 8 inch teddy bear, comes with cap and jacket |
| BR02 | BRS01 | 12 inch teddy bear | 8.99 | 12 inch teddy bear, comes with cap and jacket |
| BR03 | BRS01 | 18 inch teddy bear | 11.99 | 18 inch teddy bear, comes with cap and jacket |
| RGAN01 | DLL01 | Raggedy Ann | 4.99 | 18 inch Raggedy Ann doll |
| RYL01 | FNG01 | King doll | 9.49 | 12 inch king doll with royal garments and crown |
| RYL02 | FNG01 | Queen doll | 9.49 | 12 inch queen doll with royal garments and crown |
+---------+---------+---------------------+------------+-----------------------------------------------------------------------+
9 rows in set (0.019 sec)
如果在Django中,我应该先检索Product查询集,然后使用for循环分别提高价格并将其最终保存回数据库中。
当使用原始SQL操作时,如何使用Django ORM更新prod_price?
答案 0 :(得分:1)
您可以使用F型
from django.db.models import F
Product.objects.filter(pk__in=ids).update(prod_price=F('prod_price')+1)