django refresh form db 1

django refresh form db

def test_update_result(self):
    obj = MyModel.objects.create(val=1)
    MyModel.objects.filter(pk=obj.pk).update(val=F('val') + 1)
    # At this point obj.val is still 1, but the value in the database
    # was updated to 2. The object's updated value needs to be reloaded
    # from the database.
    obj.refresh_from_db()
    self.assertEqual(obj.val, 2)

Here is what the above code is Doing:
1. Create a MyModel object with val=1.
2. Update the object’s val to be val+1.
3. Refresh the object’s val from the database.
4. Assert that the object’s val is now 2.

Similar Posts