How to update multiple fields of a django model instance?

It’s tempting to mess with __dict__, but that won’t apply to attributes inherited from a parent class.

You can either iterate over the dict to assign to the object:

for (key, value) in my_data_dict.items():
    setattr(obj, key, value)
obj.save()

Or you can directly modify it from a queryset (making sure your query set only returns the object you’re interested in):

FooModel.objects.filter(whatever="anything").update(**my_data_dict)

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)