Reload django object from database


Is it possible to refresh the state of a django object from database? I mean behavior roughly equivalent to:

new_self = self.__class__.objects.get(
for each field of the record:
    setattr(self, field, getattr(new_self, field))

UPDATE: Found a reopen/wontfix war in the tracker: Still don't understand why the maintainers don't like this.

9/9/2018 7:46:01 PM

Accepted Answer

As of Django 1.8 refreshing objects is built in. Link to docs.

def test_update_result(self):
    obj = MyModel.objects.create(val=1)
    MyModel.objects.filter('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.
    self.assertEqual(obj.val, 2)
7/14/2015 4:22:50 PM

I've found it relatively easy to reload the object from the database like so:

x = X.objects.get(

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow