Django 1.5自定义用户模型 - 信号限制 [英] Django 1.5 custom user model - signals limitation

查看:113
本文介绍了Django 1.5自定义用户模型 - 信号限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它是在文档中写的。 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屋!

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