Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django:如何更新多个记录?_Python_Django - Fatal编程技术网

Python Django:如何更新多个记录?

Python Django:如何更新多个记录?,python,django,Python,Django,我需要更新表中的多个记录(行) 首先,我获取信息以选择要更新的行: ran_st1 = 1 ran_st2 = 1 ran_bra = 'A' pay_ide = 'FR' bra_lib = 'Polyvitamines et oligo-éléments' 然后,我选择要更新的行: rows= Randomisation.objects.filter(Q(ran_st1 = ran_st1) & Q(ran_st2 = ran_st2) & Q(ran_bra = ran_

我需要更新表中的多个记录(行)

首先,我获取信息以选择要更新的行:

ran_st1 = 1
ran_st2 = 1
ran_bra = 'A'
pay_ide = 'FR'
bra_lib = 'Polyvitamines et oligo-éléments'
然后,我选择要更新的行:

rows= Randomisation.objects.filter(Q(ran_st1 = ran_st1) & Q(ran_st2 = ran_st2) & Q(ran_bra = ran_bra) & Q(pay_ide = pay_ide))
然后,我想做一个这样的循环,但不确定:

for row in rows:
    r = get_object_or_404(Randomisation, ran_ide = row.ran_ide)
    r.ran_act = 1
    r.save()
您可以使用以下内容进行更新:

因此,这是在一个查询中完成的,而不是在多次查询中完成的,在这些查询中,您每次获取一个元素,更新该元素,然后针对该特定记录对数据库进行更新查询

Randomisation.objects.filter(
    ran_st1=ran_st1,
    ran_st2 = ran_st2,
    ran_bra = ran_bra,
    pay_ide = pay_ide
).update(ran_act=1)
UPDATE randomisation
SET ran_act = 1
WHERE ran_st1 = 1
  AND ran_st2 = 1
  AND ran_bra = 'A'
  AND pay_ide = 'FR'
  AND bra_lib = 'Polyvitamines et oligo-elements'