Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to update a field without change auto-now field?

Tags:

python

django

In a loop and automatically, that is, I always check all users once every 10 minutes, and I may need to update a bool field from them How can I not change my current field when I update that bool field ?

class Client(models.Model):
    user = models.ForeignKey ... 
    last_activity = models.DateTimeField(_("Last Activity"), auto_now=True)
    joined = models.BooleanField(_("Joined in Default Channel"), default = False)
    ...
    ... 

i want to update joined field without change last_activity.

how can i do?

like image 219
Hamidreza Avatar asked Oct 19 '25 23:10

Hamidreza


2 Answers

If you have a Client object, you can specify the fields to update with the update_fields=… parameter of the .save(…) method [Django-doc] of your model:

c1 = …  # (some client)
c1.joined = True
c1.save(update_fields=['joined'])

If you want to update Clients with a single query (or in bulk), you can work with .update(…) [Django-doc]:

c1 = Client.objects.filter(pk=some_pk).update(
    joined=True
)
like image 197
Willem Van Onsem Avatar answered Oct 21 '25 12:10

Willem Van Onsem


DateTime field is updated when .save() method is called, you can update needed field(s) using .update method.

like image 23
Yehor Sencha Avatar answered Oct 21 '25 12:10

Yehor Sencha



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!