python - 部署系统时如何向特定对象发送多个通知?
问题描述
我正在尝试向特定用户发送通知。它适用于我的本地主机,我可以向用户发送任意数量的通知,但是当我尝试部署它时,我只能向特定用户发送 1 个通知。当我尝试再次向同一用户发送通知时,我收到一条错误消息
重复的键值违反了唯一约束“user_notifications_usernotification_sender_id_key”和详细信息:键 (sender_id)=(1) 已存在。我使用 heroku 部署我的系统和 Postgresql 作为我的数据库。
我已经尝试将我的发件人关系从 更改OneToOneField
为Foreignkey
,但我仍然遇到同样的错误。
蟒蛇模型.py
class UserNotification(models.Model):
sender= models.OneToOneField(User,on_delete=models.CASCADE,related_name='user_sender',unique=True)
receiver= models.ForeignKey(User,on_delete=models.CASCADE,related_name='user_receiver',unique=False)
concern_type= models.CharField(max_length=100,choices=CHOICES)
content = models.TextField(max_length=255,blank=False)
date_sent= models.DateTimeField(default = timezone.now)
视图.py
def send_notifications(request):# form that admin can send notification to a specific user.
if request.method == 'POST':
form = SendNotificationForm(request.POST)
if form.is_valid():
instance = form.save(commit=False)
instance.sender = request.user
receiver = instance.receiver
instance.save()
messages.success(request,'Your notification was sent successfully!')
return redirect('send-notification-form')
else:
form = SendNotificationForm()
template_name = ['user-forms/send-notification-form.html']
context = {'form':form}
return render(request,template_name,context)
def user_notifications(request): # notification module of users
notifications = UserNotification.objects.filter(receiver__exact=request
.user).order_by('-date_sent')
context = { 'notifications':notifications }
template_name = ['notification.html']
return render(request,template_name,context)
表格.py
class SendNotificationForm(forms.ModelForm):
class Meta:
model = UserNotification
fields = ['receiver','concern_type','content']
解决方案
我刚刚解决了我的错误。我只是重置我的数据库并在 heroku 命令上迁移它
推荐阅读
- node.js - 当我单击按钮时,机器人再次返回相同的欢迎消息
- security - 如何在数据库中安全地存储访问密钥/私钥
- openstack - 由于网络分配失败,opnestack 实例构建失败
- email - 尝试让电子邮件脚本与 Google 表单一起使用
- javascript - ; [HPM] 尝试代理请求时出错
- python - 如何使用 Python 和 BeautifulSoup 或 Pandas 从 Finviz.com 刮取 Top Gainers 和 Top Losers 的表格行?
- forms - Symfony 4 - 未单击时未请求复选框(通过 js 广告原型添加)
- python - Pandas 根据列表项重复行
- python - 在 django admin 中的 pre_save 上获取文件 mime 类型
- javascript - 以反应形式的角度处理选择标签的 onSelect 数据的最佳方法是什么