我有一个名为User
的自定义MyUser
模型,正如文档中的full example for an custom user model所述,以及UserProfile
所谓的MyUser
在Django文档中描述的关于如何One-to-one relationship的extend an existing user model之类的内容。
Customizing authentication的文档是how to connect a post_save signal with the existing User
model的示例。
通过这样做,我将一个测试接收器放在一个由我的apps.py
文件加载的signals.py中,就像它在this SO回答中描述的那样。
def post_save_receiver(sender, instance, created, **kwargs):
send_mail('Testing', "Just testing...", 'hell@earth.tld', ['admin@heaven.tld'], fail_silently=False)
post_save.connect(post_save_receiver, sender=settings.AUTH_USER_MODEL)
class MyUserManager(BaseUserManager):
def create_user(self, email, password=None):
"""
Creates and saves a User with the given email and password.
"""
if not email:
raise ValueError('Users must have an email address')
user = self.model(
email=self.normalize_email(email),
)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, email, password):
"""
Creates and saves a superuser with the given email and password.
"""
user = self.create_user(email,
password=password,
)
user.is_admin = True
user.save(using=self._db)
return user
class MyUser(AbstractBaseUser):
email = models.EmailField(
verbose_name='email address',
max_length=255,
unique=True,
)
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
objects = MyUserManager()
USERNAME_FIELD = 'email'
def get_full_name(self):
# The user is identified by their email address
return self.email
def get_short_name(self):
# The user is identified by their email address
return self.email
def __str__(self): # __unicode__ on Python 2
return self.email
def has_perm(self, perm, obj=None):
"Does the user have a specific permission?"
# Simplest possible answer: Yes, always
return True
def has_module_perms(self, app_label):
"Does the user have permissions to view the app `app_label`?"
# Simplest possible answer: Yes, always
return True
@property
def is_staff(self):
"Is the user a member of staff?"
# Simplest possible answer: All admins are staff
return self.is_admin
def UserLogin(request):
if request.user.is_authenticated():
return HttpResponseRedirect("http://www.heaven.tld/account")
else:
if request.method == 'POST':
form = UserLoginForm(request.POST)
if form.is_valid():
username = form.cleaned_data['username']
password = form.cleaned_data['password']
user = authenticate(username=username, password=password)
if user is not None and user.is_active:
login(request, user)
return HttpResponseRedirect("http://www.heaven.tld/account")
else:
error_msg = 'Please try again!'
return render(request, "development",
{'form':form,
'error_msg':error_msg,
})
else:
error_msg = "Please try again!"
return render(request, "development",
{'form':form,
'error_msg':error_msg,
})
else:
form = UserLoginForm()
return render(request, "development", {'form': form})
现在,如果我登录,我总是收到这封邮件,但我只想在创建用户时才想要它。
答案 0 :(得分:7)
登录会导致更新last_login时间戳,从而触发保存。
正如其他人所说,你只需要使用created
参数,你的信号会收到这个参数,但你现在忽略了。
答案 1 :(得分:3)
尝试:
def post_save_receiver(sender, instance, created, **kwargs):
if created:
send_mail('Testing', "Just testing...", 'hell@earth.tld', ['admin@heaven.tld'], fail_silently=False)
post_save.connect(post_save_receiver, sender=settings.AUTH_USER_MODEL)
答案 2 :(得分:2)
我无法理解您登录时会保存的内容。您可以使用ipdb来跟踪它。顺便说一句,如果你在send_mail函数之前设置了创建的条件,那么问题就应该解决了。
angular.module('myApp')
.directive('myDirective', myDirective);
function myDirective() {
return function(scope, iElement) {
function onClick() {
alert('clicked!');
}
iElement.on('click', onClick);
scope.$on('$destroy', function() {
iElement.off('click', onClick);
});
}
}