[英]How to execute code on save in Django User model?
I would like to run some code specifically when the is_active field is changed for a Django User, similar to how the save method works for other models: 我想专门为Django用户更改is_active字段时运行一些代码,类似于save方法对其他模型的工作方式:
class Foo(models.Model):
...
def save(self, *args, **kwargs):
if self.pk is not None:
orig = Foo.objects.get(pk=self.pk)
if orig.is_active != self.is_active:
# code goes here
Can this be done through another model that is in one to one relation with the User model? 可以通过与用户模型一对一关系的另一个模型来完成此操作吗? Something like: 就像是:
class Bar(models.Model):
owner = models.OneToOneField(User, on_save=?)
...
I guess I could duplicate the is_active field on the related model and then set the is_active field on the User when saving the related model. 我想我可以在相关模型上复制is_active字段,然后在保存相关模型时在User上设置is_active字段。 But this seems like a bit of a messy solution. 但这似乎有点麻烦。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.