Zodiac
Zodiac

Reputation: 123

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..i.e

My 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.

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.

Upvotes: 1

Views: 56

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476557

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()

Upvotes: 1

Related Questions