django model post_save如果用户配置文件允许,则发送电子邮件
我正在尝试在保存对象后向用户发送电子邮件。但在发送之前,无法获取用户配置文件以查找是否允许 models.pydjango model post_save如果用户配置文件允许,则发送电子邮件,django,django-models,django-views,Django,Django Models,Django Views,我正在尝试在保存对象后向用户发送电子邮件。但在发送之前,无法获取用户配置文件以查找是否允许 models.py class UserProfile(models.Model): user = models.OneToOneField(User) ... notifications = models.BooleanField(default=True) ... class Follow(models.Model): who = models.Foreign
class UserProfile(models.Model):
user = models.OneToOneField(User)
...
notifications = models.BooleanField(default=True)
...
class Follow(models.Model):
who = models.ForeignKey(User, related_name='he_follow')
whom = models.ForeignKey(User, related_name='him_follow')
...
def send_notification(sender, created, **kwargs):
if created:
obj = kwargs['instance']
check_it = obj.whom.get_profile().notifications
if check_it == True:
#rest code for sending emails works
else:
pass
post_save.connect(send_notification)
这个return me error“LogEntry”对象没有属性“who”
,所以我认为这是因为post\u save中没有发送者。但在我将post_保存行更改为`
post_save.connect(send_notification, sender=Follow)
django崩溃,出现错误
name错误:未定义名称“Follow”
检查您需要/发送到发送通知的参数
使用post_保存信号()发送的参数:
- 发送者:模型类
- 实例:正在保存的实际实例
- 创建:布尔值;如果创建了新记录,则为True
- 原始:布尔值;如果模型完全按照显示的方式保存(即加载夹具时),则为True。不应查询/修改数据库中的其他记录,因为数据库可能尚未处于一致状态
- 使用:正在使用的数据库别名
您的send_notification
方法在signals.py
文件中应该是这样的:
from yourproject.yourapp.models import Follow
...
def send_notification(sender, **kwargs):
if kwargs['created']:
check_it = kwargs['instance'].whom.get_profile().notifications
if check_it == True:
#rest code for sending emails works
else:
pass
post_save.connect(send_notification, sender=Follow)
检查您需要/发送到发送通知的参数
使用post_保存信号()发送的参数:
- 发送者:模型类
- 实例:正在保存的实际实例
- 创建:布尔值;如果创建了新记录,则为True
- 原始:布尔值;如果模型完全按照显示的方式保存(即加载夹具时),则为True。不应查询/修改数据库中的其他记录,因为数据库可能尚未处于一致状态
- 使用:正在使用的数据库别名
您的send_notification
方法在signals.py
文件中应该是这样的:
from yourproject.yourapp.models import Follow
...
def send_notification(sender, **kwargs):
if kwargs['created']:
check_it = kwargs['instance'].whom.get_profile().notifications
if check_it == True:
#rest code for sending emails works
else:
pass
post_save.connect(send_notification, sender=Follow)
真正的问题是你的功能是否一致。你把它放在跟随模型下,它必须在模型之外
class Follow(models.Model):
who = models.ForeignKey(User, related_name='he_follow')
whom = models.ForeignKey(User, related_name='him_follow')
...
//align with Follow model don't put it inside
def send_notification(sender, created, **kwargs):
if created:
obj = kwargs['instance']
check_it = obj.whom.get_profile().notifications
if check_it == True:
#rest code for sending emails works
else:
pass
post_save.connect(send_notification, sender=Follow)
真正的问题是你的功能是否一致。你把它放在跟随模型下,它必须在模型之外
class Follow(models.Model):
who = models.ForeignKey(User, related_name='he_follow')
whom = models.ForeignKey(User, related_name='him_follow')
...
//align with Follow model don't put it inside
def send_notification(sender, created, **kwargs):
if created:
obj = kwargs['instance']
check_it = obj.whom.get_profile().notifications
if check_it == True:
#rest code for sending emails works
else:
pass
post_save.connect(send_notification, sender=Follow)
谢谢你,Daniel,但是我收到了相同的错误,“'LogEntry'对象没有属性'Where',你的post\u save.connect(send\u notification,sender=Follow)
行在你的models.py
文件中?如果它是像signals.py
这样的其他地方,那么听起来你只是没有在signals.py
中导入Follow
类模型。是的,它包括上面的意图,但是当我添加`sender=Follow`django时,正如我上面解释的那样崩溃了。谢谢Daniel对齐是关键,但是你帮了我很多忙谢谢再次感谢您,Daniel,但我收到了相同的错误,“'LogEntry'对象没有属性'Where',您的post\u save.connect(send\u notification,sender=Follow)
行在您的models.py
文件中?如果它是像signals.py
这样的其他地方,那么听起来你只是没有在signals.py
中导入Follow
类模型。是的,它包括上面的意图,但是当我添加`sender=Follow`django时,正如我上面解释的那样崩溃了。谢谢Daniel对齐是关键,但是你帮了我很多忙谢谢再一次。