【发布时间】:2012-12-28 16:55:09
【问题描述】:
我已经通过 pip 安装了django-notifications,将该应用添加到我的INSTALLED_APPS:
INSTALLED_APPS = (
'django.contrib.auth',
...
'notifications',
...
)
更新了我的 URLConf:
import notifications
urlpatterns = patterns('',
...
('^inbox/notifications/', include(notifications.urls), namespace='notifications'),
...
)
因为我安装了south,所以我迁移了架构:
$ python manage.py migrate notifications
...
...
$ python manage.py migrate --list
notifications
(*) 0001_initial
(*) 0002_auto__add_field_notification_data
(*) 0003_auto__add_field_notification_unread
(*) 0004_convert_readed_to_unread
(*) 0005_auto__del_field_notification_readed
(*) 0006_auto__add_field_notification_level
...
现在,我正在尝试通过 Django shell 手动创建通知,但我收到了 IntegrityError:
[1]: from django.contrib.auth.models import User
[2]: from notifications import notify
[3]: recipient = User.objects.get(username="admin")
[4]: sender = User.objects.get(username="guest")
[5]: notify.send(sender, verb='A test', recipient=recipient)
...
...
...
IntegrityError: (1452, 'Cannot add or update a child row: a foreign key constraint fails (`myapp`.`notifications_notification`, CONSTRAINT `recipient_id_refs_id_5c79cb54` FOREIGN KEY (`recipient_id`) REFERENCES `auth_user` (`id`))')
发生了什么事? recipient 和 sender 都属于 auth_user 表。任何帮助将不胜感激。
【问题讨论】:
-
第一个参数应该是收件人,使用
actor=sender或者重新排序参数notify.send(recipient, sender, verb='A test') -
@AamirAdnan 这也不起作用。如果尝试
notify.send(recipient, actor=sender, verb='A test'),那么我会得到KeyError "recipient"。如果我使用notify.send(recipient, sender, verb='A test')我会得到TypeError: send() takes exactly 2 arguments (4 given) -
试试这个
notify.send(recipient=recipient, sender=sender, verb='A test')这应该可以。 -
@AamirAdnan 我再次得到 IntegrityError。谢谢,看来问题出在其他地方
-
@AamirAdnan 不,它期待收件人关键字,所以我收到 KeyError "recipient"
标签: python django django-notification