我正在使用django-notification创建通知。基于这是文件,我提出:
url(r'^inbox/notifications/', include(notifications.urls, namespace='notifications')),在我的urls.py里。通过在我的views.py中使用以下方法生成测试通知:
guy = User.objects.get(username = 'SirSaleh')
notify.send(sender=User, recipient=guy, verb='you visted the site!')我可以很容易地在这个url中得到未读通知的数量:
http://127.0.0.1:8000/inbox/notifications/api/unread_count/它按我的意愿返回{"unread_count": 1}。但是使用/api/unread_list/,我无法获得通知列表,我得到了以下错误:
ValueError at /inbox/notifications/
invalid literal for int() with base 10: '<property object at 0x7fe1b56b6e08>'作为我在使用django-notifications的初学者,任何帮助都将不胜感激。
全TraceBack
环境: 请求方法:获取请求URL:名单/ Django版本: 2.0.2 Python版本: 3.5.2已安装的应用程序:'django.contrib.admin','django.contrib.auth',‘django.contrib.contenttype’,‘django.contrib.contenttype’,'django.contrib.messages',‘django.contrib.Messages’,'django.contrib.staticfiles',‘django.contrib.site’,‘django.contrib.auth’,'allauth',‘allauth.Social’,‘监护人’,'axes','django_otp','django_otp.plugins.otp_static','django_otp.plugins.otp_totp','two_factor',‘邀请’,'avatar','imagekit','import_export','djmoney','captcha','dal',‘dal_select2 2’,‘小部件_微调’,‘括号’,'django_tables2','phonenumber_field','hitcount','el_pagination',“维护模式”、“通知”、“数学过滤器”、“myproject_web”、“订单”、“照片库”、“搜索”、“社会”、“用户myproject”、“用户身份验证”、“用户授权”、“UserProfile”已安装的中间件:'django.middleware.security.SecurityMiddleware','django.contrib.sessions.middleware.SessionMiddleware',‘django.medileware.locale.Locale中间件’、‘django.medileware.公共、中间件’、‘django.medileware.csrf.csrfView中间件’,'django.contrib.auth.middleware.AuthenticationMiddleware','django.contrib.messages.middleware.MessageMiddleware','django.middleware.clickjacking.XFrameOptionsMiddleware',‘django_otp.medileware.OTP中间件’,'maintenance_mode.middleware.MaintenanceModeMiddleware‘ 回溯: 在内部35中文件"/home/saleh/Projects/myproject_web/lib/python3.5/site-packages/django/core/handlers/exception.py“。响应=get_response(请求) "/home/saleh/Projects/myproject_web/lib/python3.5/site-packages/django/core/handlers/base.py“文件在_get_response 128中。响应= self.process_exception_by_middleware(e,请求) "/home/saleh/Projects/myproject_web/lib/python3.5/site-packages/django/core/handlers/base.py“文件在_get_response 126中。response =wrapped_callback(请求、*callback_args、**callback_kwargs) "/home/saleh/Projects/myproject_web/lib/python3.5/site-packages/notifications/views.py“文件在live_unread_notification_list 164中。如果n.actor: 文件"/home/saleh/Projects/myproject_web/lib/python3.5/site-packages/django/contrib/contenttypes/fields.py“in get 253。rel_obj = ct.get_object_for_this_type(pk=pk_val) "/home/saleh/Projects/myproject_web/lib/python3.5/site-packages/django/contrib/contenttypes/models.py“文件在get_object_for_this_type 169中。返回self.model_class()._base_manager.using(self._state.db).get(**kwargs) 在get 394中文件"/home/saleh/Projects/myproject_web/lib/python3.5/site-packages/django/db/models/query.py“。克隆= self.filter(*args,**kwargs) 在过滤器836中文件"/home/saleh/Projects/myproject_web/lib/python3.5/site-packages/django/db/models/query.py“。返回self._filter_or_exclude(False,*args,**kwargs) 文件"/home/saleh/Projects/myproject_web/lib/python3.5/site-packages/django/db/models/query.py“在_filter_or_exclude 854中。clone.query.add_q(Q(*args,**kwargs)) 文件"/home/saleh/Projects/myproject_web/lib/python3.5/site-packages/django/db/models/sql/query.py“在add_q 1253中。子句,_= self._add_q(q_object,self.used_aliases) 文件"/home/saleh/Projects/myproject_web/lib/python3.5/site-packages/django/db/models/sql/query.py“在_add_q 1277中。split_subq=split_subq, 文件"/home/saleh/Projects/myproject_web/lib/python3.5/site-packages/django/db/models/sql/query.py“在build_filter 1215中。条件=self.build_lookup(查找,col,值) 文件"/home/saleh/Projects/myproject_web/lib/python3.5/site-packages/django/db/models/sql/query.py“在build_lookup 1085中。查找= lookup_class(lhs,rhs) 文件"/home/saleh/Projects/myproject_web/lib/python3.5/site-packages/django/db/models/lookups.py“in init 18. self.rhs = self.get_prep_lookup() "/home/saleh/Projects/myproject_web/lib/python3.5/site-packages/django/db/models/lookups.py“文件在get_prep_lookup 68中。返回self.lhs.output_field.get_prep_value(self.rhs) 文件"/home/saleh/Projects/myproject_web/lib/python3.5/site-packages/django/db/models/fields/init.py“在get_prep_value 947中。返回int(值) 异常类型: ValueError at /inbox/ValueError/api/unread_list/ Exception值:带基10的int()的文本无效
发布于 2018-07-02 14:25:46
糟了!这是我的错。我终于找到了问题所在。actor_object_id是notifications_notification表的字段,User.objects.get(username = 'SirSaleh')保存在其中。它应该是Interger (演员的user_id)。
因此,我将前面的表“将实例更改为User.objects.get(username = 'SirSaleh')”删除为“用户ID”。问题解决了。
为什么actor_object_id 的类型是CharField (varchar)?(至少我不知道);)
发布于 2018-07-03 11:45:21
actor_object_id需要是一个CharField,以支持基于UUID的主键。
发布于 2018-12-04 21:43:13
这是老生常谈,但我碰巧知道答案。
在你的代码中,你写道:
guy = User.objects.get(username = 'SirSaleh')
notify.send(sender=User, recipient=guy, verb='you visted the site!')您表示希望guy是您的发件人,但是在notify.send中,您将发送方标记为一般的User对象,而不是guy。
因此,将代码更改为:
guy = User.objects.get(username = 'SirSaleh')
notify.send(sender=guy, recipient=guy, verb='you visted the site!')通知将接受用户对象guy,推断ID并相应地将其存储在数据库中。
https://stackoverflow.com/questions/51129487
复制相似问题