简体   繁体   English

更新 django 中的查询集

[英]Update a queryset in django

I need to update the data on the database once I get all the values of that data with current DateTime..ie一旦我使用当前 DateTime 获取该数据的所有值,我需要更新数据库中的数据。即

My Model:我的 Model:

class Load_Balancing(models.Model):

    instance_name = models.CharField(max_length=100)
    instance_visit = models.DateTimeField(null=True)
    sequence = models.IntegerField()

I want to get the value with the last sequence number inserted in the database and update its time to the current time.我想获取插入数据库的最后一个序列号的值,并将其时间更新为current时间。

I tried:我试过了:

instances = Load_Balancing.objects.all().order_by("-sequence")[:1]
a = Load_Balancing.objects.filter(sequence=instances.sequence).update(instance_visit= datetime.datetime.now())

But it's not working.但它不起作用。

If you want to update the latest one, you can do that with:如果你想更新最新的,你可以这样做:

from django.utils import timezone

instance = Load_Balancing.objects.all().latest('sequence')
instance.instance_visit = timezone.now()
instance.save()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM