从数据库重新加载django对象

gre*_*rep 141 python django django-models

是否可以从数据库刷新django对象的状态?我的意思是行为大致相当于:

new_self = self.__class__.objects.get(pk=self.pk)
for each field of the record:
    setattr(self, field, getattr(new_self, field))
Run Code Online (Sandbox Code Playgroud)

更新:在跟踪器中找到了重新打开/未修正的战争:http://code.djangoproject.com/ticket/901 .还是不明白为什么维护者不喜欢这个.

Tim*_*her 231

从Django 1.8开始,内置刷新对象.链接到文档.

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)
Run Code Online (Sandbox Code Playgroud)


Ror*_*ory 27

我发现从数据库重新加载对象比较容易:

x = X.objects.get(id=x.id)
Run Code Online (Sandbox Code Playgroud)

  • 是的,但是......之后你必须更新对这个对象的所有引用.不是很方便且容易出错. (16认同)
  • 来自django.db.models.loading import get_model; instance = get_model(instance).objects.get(pk = instance.pk) (3认同)
  • 我认为 `refresh_from_db` 解决了所有这些问题。 (3认同)
  • 当Celery在django之外的数据库中更新我的对象时发现这是必要的,django显然保留了对象的缓存,因为它不知道它已经改变了. (2认同)

Elo*_*off 16

在参考@ grep的评论时,不应该这样做:

# Put this on your base model (or monkey patch it onto django's Model if that's your thing)
def reload(self):
    new_self = self.__class__.objects.get(pk=self.pk)
    # You may want to clear out the old dict first or perform a selective merge
    self.__dict__.update(new_self.__dict__)

# Use it like this
bar.foo = foo
assert bar.foo.pk is None
foo.save()
foo.reload()
assert bar.foo is foo and bar.foo.pk is not None
Run Code Online (Sandbox Code Playgroud)

  • Django现在提供`refresh_from_db`方法. (10认同)

Ron*_*Ron 7

正如@Flimm指出的,这是一个非常棒的解决方案:

foo.refresh_from_db()
Run Code Online (Sandbox Code Playgroud)

这会将所有数据从数据库重新加载到对象中。