[Fixed]-ValueError: "<Notification: Notification object>" needs to have a value for field "notification" before this many-to-many relationship can be used

1👍

You can’t associate Notification with a User until it’s been saved.

So you have to save Notification first then you can add receivers

notification = Notification(
    sender=sender,
    type=Notification.TYPE_CHAT_MESSAGE,
    data=json.dumps(data)
).save()
# If chat.get_other_users(sender) return a queryset
receivers = chat.get_other_users(sender)
for receiver in receivers:
    notification.receivers.add(receiver)
# or you can also simply assign the whole list as it's already empty after new create
# >>> notification.receivers = recievers
notification.send()

Leave a comment