关于 post_save 信号和创建的参数
docs 说:
post_save
django.db.models.signals.post_save
created
A boolean; True if a -new- record was create.
我有这个:
from django.db.models.signals import post_save
def handle_new_user(sender, instance, created, **kwargs):
print "--------> save() "+str(created)
post_save.connect(handle_new_user, sender=User)
当我在 shell 中执行:
u = User(username="cat")
u.save()
>>> --------> save() True
u.username = "dog"
u.save()
>>> --------> save() True
我期望 >>> --------> save() False 当我第二次 save() 时因为是更新?不是?
the docs says:
post_save
django.db.models.signals.post_save
created
A boolean; True if a -new- record was create.
and I have this:
from django.db.models.signals import post_save
def handle_new_user(sender, instance, created, **kwargs):
print "--------> save() "+str(created)
post_save.connect(handle_new_user, sender=User)
when I do in shell:
u = User(username="cat")
u.save()
>>> --------> save() True
u.username = "dog"
u.save()
>>> --------> save() True
I expect a >>> --------> save() False
when I save() the second time because is an update? not?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
似乎您已经实现了自己的用户,它对用户名没有唯一的约束?
Seems like you have implemented your own User which doesn't have a unique constraint on username?
我建议您使用
User.objects.create_user
来避免批量操作。I suggest you use
User.objects.create_user
to avoid bulk operation.