Django 1.5自定义用户模型 - 信号限制 [英] Django 1.5 custom user model - signals limitation
问题描述
它是在文档中写的。 a>:
自定义用户模型的另一个限制是您不能使用django.contrib.auth.get_user_model()作为信号处理程序的发送者或目标。相反,您必须使用生成的用户模型注册处理程序。有关注册发送信号的更多信息,请参阅信号。
我想这意味着您可以执行以下操作:
$ b来自django.contrib.auth的$ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
$ b $ post_save,sender = User)
def user_saved(sender = None,instance = None,** kwargs):
#something
不是吗?我只是想知道我是否明白(我不明白为什么他们说这是一个限制,但无论如何,只是想检查)。
应该有效。
我认为他们的意思是使用与发件人相同的功能
在doc中:
作为信号处理程序的发件人或目标。相反,您必须向结果的用户模型
注册It's written in the doc that:
Another limitation of custom User models is that you can’t use django.contrib.auth.get_user_model() as the sender or target of a signal handler. Instead, you must register the handler with the resulting User model. See Signals for more information on registering an sending signals.
I guess it means you can do the following:
from django.contrib.auth import get_user_model User = get_user_model() @receiver(post_save, sender=User) def user_saved(sender=None, instance=None, **kwargs): # something
Isn't it? I'm just wondering if I understand well (I don't understand why they say it's a "limitation", but whatever, just want to check).
解决方案That should work. I think they mean to use the same function as sender
in doc:
as the sender or target of a signal handler. Instead, you must register the handler with the resulting User model
这篇关于Django 1.5自定义用户模型 - 信号限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!