Django:在模型更新之前,我想“看”其以前的属性 [英] Django: Before a model is updated, I'd like to "look at" its previous attributes

查看:133
本文介绍了Django:在模型更新之前,我想“看”其以前的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当在Django模型上执行更新/创建时( .save())我希望能够进入并比较一些特定的属性他们以前设置了什么(如果它们以前存在的话)。



我正在考虑预先保存信号,查看原始模型,执行 .objects .get(instance.id),但感觉很浪费。另外,在 pre_save()中已经发生了验证?

解决方案

关于模型验证


请注意,当您调用模型的save()方法时,将不会自动调用full_clean()


然后,关于预先保存信号,请注意,您将获取正在保存的实例作为消息的参数发送。由于您的模型的前一版本仅存在于数据库中,我看不到您可以在哪里获得以前的属性值...



告诉你为什么要这么做,所以很难说,但现在我正在考虑的其他解决方案:

  *定义每当您感兴趣的属性被修改时发送的自定义信号...该信号将发送两个参数:新值,旧值
*在设置属性时直接执行检查

如果您提供更多详细信息,可能会更容易...



编辑:



没错...如果您发出自定义的foo_has_updated,您将无法确保修改被保存。



在这种情况下,我想您可以在初始化实例时缓存感兴趣的变量,并捕获保存后或预先保存的信号。

  *使用预先保存,您将会abl e预处理数据,但保存操作可能会失败
*通过后保存,您将确保数据已保存。

缓存变量可以这样完成:



$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $:::::::::::::::::::::::::::::::::::::::::::::::::::::
super(CachedModel,self).__ init __(* args,** kwargs)
self.var_cache = {}
for self in Cached_vars:
self.var_cache [var ] = copy.copy(getattr(self,var))

或类似的东西... Then ,在你的信号处理程序中:

  def post_save_handler(sender,** kwargs):
instance = kwargs [instance ]
[(instance.var_cache [var],getattr(instance,var))for var in instance.cached_var]
#[(< initial value>< saved value>)

你有你需要的东西(我想)!!!


When an update/create is performed on a Django model (.save()) I would like to be able to "step in" and compare some particular attributes to what they were set to previously (if they previously existed at all).

I'm thinking Pre-Save Signals, with a look-up to the original model doing a .objects.get(instance.id), but that feels wasteful. Also, has the validation already happened in pre_save()?

解决方案

about model validation :

Note that full_clean() will not be called automatically when you call your model’s save() method

Then, about the pre-save signal, note that you get the instance that is being saved sent as a parameter with the message. As the former version of your model exists only in the database, I don't see where else you could get the previous value of the attributes ...

You don't tell why you want to do this so it's hard to say, but other solutions I'm thinking of right now :

* defining a custom signal that is sent everytime the attributes you are interested in are modified... This signal would then send two arguments : new value, old value
* perform the check directly when setting the attributes

If you give more details, it might be easier...

EDIT :

That's right ... If you emit a custom 'foo_has_updated', you will not be sure that the modification is saved.

In this case, I guess you could cache the variables that interest you while initializing the instance, and catch the post-save OR pre-save signal.

* With pre-save, you would be able to pre-process the data, but the saving operation might fail
* With post-save, you would be sure that the data has been saved.

Caching your variables could be done like this :

class CachedModel(models.Model):
    cached_vars = [var1, var2, varN]
    def __init__(self, *args, **kwargs):
        super(CachedModel, self).__init__(*args, **kwargs)
        self.var_cache = {}
        for var in self.cached_vars:
            self.var_cache[var] = copy.copy(getattr(self, var))

Or something like this ... Then, in your signal handler :

def post_save_handler(sender, **kwargs):
    instance = kwargs["instance"]
    [(instance.var_cache[var], getattr(instance, var)) for var in instance.cached_var]
    #[(<initial value>, <saved value>)

And you got what you needed (I think)!!!

这篇关于Django:在模型更新之前,我想“看”其以前的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆