[PYTHON] [django] model mods may or may not be updated

Introduction

Did you know that model modifications may or may not be updated? I didn't know I checked it

When modified is updated

user = User.objects.get(pk=1)
user.name = 'hoge'
user.save()

Well, it ’s a common one.

If modified is not updated

User.objects.filter(prefecture_id=1).update(prefecture_name='Hokkaido')

It will not be updated using the ʻupdate` method!

modified to update

User.objects.filter(prefecture_id=1).update(prefecture_name='Hokkaido', modified=datetime.datetime.now())

Must be explicitly specified like this

users = []
for user in User.objects.filter(prefecture_id=1):
    user.prefecture_name ='Hokkaido'
    user.modified = datetime.datetime.now()
    users.append(user)
User.objects.bulk_update(users, fields=['prefecture_name', 'modified'])

bulk_update is not updated as well, so you need to specify it explicitly

Recommended Posts

[django] model mods may or may not be updated
Django's auto_now may not be updated
PyQtGraph may not be available in the interpreter.
Qiitadon icon may not be changed by API
Django model: ManyToManyField