gpt4 book ai didi

python - Django:save() vs update() 来更新数据库?

转载 作者:行者123 更新时间:2023-11-28 19:35:27 31 4
gpt4 key购买 nike

我正在编写一个 Django 应用程序,我需要一个函数来更新数据库中的字段。是否有任何理由采用这些方法中的一种而不是另一种?

def save_db_field(name,field,value):
obj = MyModel.objects.get(name=name)
obj.field = value
obj.save()

def update_db_field(name,field,value):
MyModel.objects.get(name=name).update(field=value)

似乎第二个更好,因为它在一次数据库调用中完成,而不是两次。为什么先抓取再更新更好?

最佳答案

有几个关键区别。

update 用于查询集,因此可以一次更新多个对象。

作为@FallenAngel指出,自定义 save() 方法的触发方式存在差异,但记住 signalsModelManagers 也很重要。我构建了一个小型测试应用程序来展示一些有值(value)的差异。我使用的是 Python 2.7.5、Django==1.7.7 和 SQLite,请注意,最终的 SQL 可能会因不同版本的 Django 和不同的数据库引擎而有所不同。

好的,这是示例代码。

models.py:

from __future__ import print_function
from django.db import models
from django.db.models import signals
from django.db.models.signals import pre_save, post_save
from django.dispatch import receiver

__author__ = 'sobolevn'

class CustomManager(models.Manager):
def get_queryset(self):
super_query = super(models.Manager, self).get_queryset()
print('Manager is called', super_query)
return super_query


class ExtraObject(models.Model):
name = models.CharField(max_length=30)

def __unicode__(self):
return self.name


class TestModel(models.Model):

name = models.CharField(max_length=30)
key = models.ForeignKey('ExtraObject')
many = models.ManyToManyField('ExtraObject', related_name='extras')

objects = CustomManager()

def save(self, *args, **kwargs):
print('save() is called.')
super(TestModel, self).save(*args, **kwargs)

def __unicode__(self):
# Never do such things (access by foreing key) in real life,
# because it hits the database.
return u'{} {} {}'.format(self.name, self.key.name, self.many.count())


@receiver(pre_save, sender=TestModel)
@receiver(post_save, sender=TestModel)
def reicever(*args, **kwargs):
print('signal dispatched')

views.py:

def index(request):
if request and request.method == 'GET':

from models import ExtraObject, TestModel

# Create exmple data if table is empty:
if TestModel.objects.count() == 0:
for i in range(15):
extra = ExtraObject.objects.create(name=str(i))
test = TestModel.objects.create(key=extra, name='test_%d' % i)
test.many.add(test)
print test

to_edit = TestModel.objects.get(id=1)
to_edit.name = 'edited_test'
to_edit.key = ExtraObject.objects.create(name='new_for')
to_edit.save()

new_key = ExtraObject.objects.create(name='new_for_update')
to_update = TestModel.objects.filter(id=2).update(name='updated_name', key=new_key)
# return any kind of HttpResponse

导致这些 SQL 查询的结果:

# to_edit = TestModel.objects.get(id=1):
QUERY = u'SELECT "main_testmodel"."id", "main_testmodel"."name", "main_testmodel"."key_id"
FROM "main_testmodel"
WHERE "main_testmodel"."id" = %s LIMIT 21'
- PARAMS = (u'1',)

# to_edit.save():
QUERY = u'UPDATE "main_testmodel" SET "name" = %s, "key_id" = %s
WHERE "main_testmodel"."id" = %s'
- PARAMS = (u"'edited_test'", u'2', u'1')

# to_update = TestModel.objects.filter(id=2).update(name='updated_name', key=new_key):
QUERY = u'UPDATE "main_testmodel" SET "name" = %s, "key_id" = %s
WHERE "main_testmodel"."id" = %s'
- PARAMS = (u"'updated_name'", u'3', u'2')

我们只有一个查询 update() 和两个 save()

接下来,让我们谈谈重写save() 方法。显然,save() 方法只调用一次。值得一提的是,.objects.create() 还调用了save() 方法。

但是 update() 不会在模型上调用 save()。如果没有为 update() 调用 save() 方法,那么信号也不会被触发。输出:

Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

# TestModel.objects.get(id=1):
Manager is called [<TestModel: edited_test new_for 0>]
Manager is called [<TestModel: edited_test new_for 0>]
save() is called.
signal dispatched
signal dispatched

# to_update = TestModel.objects.filter(id=2).update(name='updated_name', key=new_key):
Manager is called [<TestModel: edited_test new_for 0>]

如您所见,save() 触发了 Managerget_queryset() 两次。当 update() 只有一次。

分辨率。如果您需要“静默”更新您的值,而不调用 save() - 使用 update。用例:last_seen 用户字段。当您需要正确更新模型时,请使用 save()

关于python - Django:save() vs update() 来更新数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30449960/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com