Reload django object from database

前端 未结 4 838
囚心锁ツ
囚心锁ツ 2020-12-13 16:30

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(pk=self.pk         


        
4条回答
  •  春和景丽
    2020-12-13 17:15

    As @Flimm pointed out, this is a really awesome solution:

    foo.refresh_from_db()
    

    This reloads all data from the database into the object.

提交回复
热议问题