以 10 为基数的 int() 的 Django 通知无效文字
Django-notification invalid literal for int() with base 10
我正在使用 django-notification
创建通知。基于 it's documention 我推杆:
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/
它return {"unread_count": 1}
如我所愿。但是 /api/unread_list/
我无法获取通知列表,我收到此错误:
ValueError at /inbox/notifications/
invalid literal for int() with base 10: '<property object at 0x7fe1b56b6e08>'
我是初学者 django-notifications
任何帮助将不胜感激。
完整追溯
Environment:
Request Method: GET Request URL:
http://127.0.0.1:8000/inbox/notifications/api/unread_list/
Django Version: 2.0.2 Python Version: 3.5.2 Installed Applications:
['django.contrib.admin', 'django.contrib.auth',
'django.contrib.contenttypes', 'django.contrib.sessions',
'django.contrib.messages', 'django.contrib.staticfiles',
'django.contrib.sites', 'django.forms', 'rest_framework',
'allauth', 'allauth.account', 'allauth.socialaccount', 'guardian',
'axes', 'django_otp', 'django_otp.plugins.otp_static',
'django_otp.plugins.otp_totp', 'two_factor', 'invitations',
'avatar', 'imagekit', 'import_export', 'djmoney', 'captcha',
'dal', 'dal_select2', 'widget_tweaks', 'braces', 'django_tables2',
'phonenumber_field', 'hitcount', 'el_pagination',
'maintenance_mode', 'notifications', 'mathfilters',
'myproject_web', 'Order', 'PhotoGallery', 'Search', 'Social',
'UserAccount', 'UserAuthentication', 'UserAuthorization',
'UserProfile'] Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django_otp.middleware.OTPMiddleware',
'maintenance_mode.middleware.MaintenanceModeMiddleware']
Traceback:
File
"/home/saleh/Projects/myproject_web/lib/python3.5/site-packages/django/core/handlers/exception.py"
in inner
35. response = get_response(request)
File
"/home/saleh/Projects/myproject_web/lib/python3.5/site-packages/django/core/handlers/base.py"
in _get_response
128. response = self.process_exception_by_middleware(e, request)
File
"/home/saleh/Projects/myproject_web/lib/python3.5/site-packages/django/core/handlers/base.py"
in _get_response
126. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File
"/home/saleh/Projects/myproject_web/lib/python3.5/site-packages/notifications/views.py"
in live_unread_notification_list
164. if n.actor:
File
"/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)
File
"/home/saleh/Projects/myproject_web/lib/python3.5/site-packages/django/contrib/contenttypes/models.py"
in get_object_for_this_type
169. return self.model_class()._base_manager.using(self._state.db).get(**kwargs)
File
"/home/saleh/Projects/myproject_web/lib/python3.5/site-packages/django/db/models/query.py"
in get
394. clone = self.filter(*args, **kwargs)
File
"/home/saleh/Projects/myproject_web/lib/python3.5/site-packages/django/db/models/query.py"
in filter
836. return self._filter_or_exclude(False, *args, **kwargs)
File
"/home/saleh/Projects/myproject_web/lib/python3.5/site-packages/django/db/models/query.py"
in _filter_or_exclude
854. clone.query.add_q(Q(*args, **kwargs))
File
"/home/saleh/Projects/myproject_web/lib/python3.5/site-packages/django/db/models/sql/query.py"
in add_q
1253. clause, _ = self._add_q(q_object, self.used_aliases)
File
"/home/saleh/Projects/myproject_web/lib/python3.5/site-packages/django/db/models/sql/query.py"
in _add_q
1277. split_subq=split_subq,
File
"/home/saleh/Projects/myproject_web/lib/python3.5/site-packages/django/db/models/sql/query.py"
in build_filter
1215. condition = self.build_lookup(lookups, col, value)
File
"/home/saleh/Projects/myproject_web/lib/python3.5/site-packages/django/db/models/sql/query.py"
in build_lookup
1085. lookup = lookup_class(lhs, rhs)
File
"/home/saleh/Projects/myproject_web/lib/python3.5/site-packages/django/db/models/lookups.py"
in init
18. self.rhs = self.get_prep_lookup()
File
"/home/saleh/Projects/myproject_web/lib/python3.5/site-packages/django/db/models/lookups.py"
in get_prep_lookup
68. return self.lhs.output_field.get_prep_value(self.rhs)
File
"/home/saleh/Projects/myproject_web/lib/python3.5/site-packages/django/db/models/fields/init.py"
in get_prep_value
947. return int(value)
Exception Type: ValueError at /inbox/notifications/api/unread_list/
Exception Value: invalid literal for int() with base 10: ''
糟糕!这是我的错误。
我终于找出问题所在。 actor_object_id
是notifications_notification
table的字段,User.objects.get(username = 'SirSaleh')
保存在里面。应该是Interger
(演员的user_id
)。
所以我放弃了以前的 table 将实例更改为 User.objects.get(username = 'SirSaleh')
到用户 ID。问题已解决。
那么为什么 actor_object_id
的类型是 CharField (varchar)? (至少我不知道) ;))
actor_object_id 需要是 CharField 才能支持基于 UUID 的主键。
这是旧的,但我恰好知道答案。
在您的代码中,您写道:
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 并将其相应地存储在数据库中。
我正在使用 django-notification
创建通知。基于 it's documention 我推杆:
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/
它return {"unread_count": 1}
如我所愿。但是 /api/unread_list/
我无法获取通知列表,我收到此错误:
ValueError at /inbox/notifications/
invalid literal for int() with base 10: '<property object at 0x7fe1b56b6e08>'
我是初学者 django-notifications
任何帮助将不胜感激。
完整追溯
Environment:
Request Method: GET Request URL: http://127.0.0.1:8000/inbox/notifications/api/unread_list/
Django Version: 2.0.2 Python Version: 3.5.2 Installed Applications: ['django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'django.contrib.sites', 'django.forms', 'rest_framework', 'allauth', 'allauth.account', 'allauth.socialaccount', 'guardian', 'axes', 'django_otp', 'django_otp.plugins.otp_static', 'django_otp.plugins.otp_totp', 'two_factor', 'invitations', 'avatar', 'imagekit', 'import_export', 'djmoney', 'captcha', 'dal', 'dal_select2', 'widget_tweaks', 'braces', 'django_tables2', 'phonenumber_field', 'hitcount', 'el_pagination', 'maintenance_mode', 'notifications', 'mathfilters', 'myproject_web', 'Order', 'PhotoGallery', 'Search', 'Social', 'UserAccount', 'UserAuthentication', 'UserAuthorization', 'UserProfile'] Installed Middleware: ['django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.locale.LocaleMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', 'django_otp.middleware.OTPMiddleware', 'maintenance_mode.middleware.MaintenanceModeMiddleware']
Traceback:
File "/home/saleh/Projects/myproject_web/lib/python3.5/site-packages/django/core/handlers/exception.py" in inner 35. response = get_response(request)
File "/home/saleh/Projects/myproject_web/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response 128. response = self.process_exception_by_middleware(e, request)
File "/home/saleh/Projects/myproject_web/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response 126. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/saleh/Projects/myproject_web/lib/python3.5/site-packages/notifications/views.py" in live_unread_notification_list 164. if n.actor:
File "/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)
File "/home/saleh/Projects/myproject_web/lib/python3.5/site-packages/django/contrib/contenttypes/models.py" in get_object_for_this_type 169. return self.model_class()._base_manager.using(self._state.db).get(**kwargs)
File "/home/saleh/Projects/myproject_web/lib/python3.5/site-packages/django/db/models/query.py" in get 394. clone = self.filter(*args, **kwargs)
File "/home/saleh/Projects/myproject_web/lib/python3.5/site-packages/django/db/models/query.py" in filter 836. return self._filter_or_exclude(False, *args, **kwargs)
File "/home/saleh/Projects/myproject_web/lib/python3.5/site-packages/django/db/models/query.py" in _filter_or_exclude 854. clone.query.add_q(Q(*args, **kwargs))
File "/home/saleh/Projects/myproject_web/lib/python3.5/site-packages/django/db/models/sql/query.py" in add_q 1253. clause, _ = self._add_q(q_object, self.used_aliases)
File "/home/saleh/Projects/myproject_web/lib/python3.5/site-packages/django/db/models/sql/query.py" in _add_q 1277. split_subq=split_subq,
File "/home/saleh/Projects/myproject_web/lib/python3.5/site-packages/django/db/models/sql/query.py" in build_filter 1215. condition = self.build_lookup(lookups, col, value)
File "/home/saleh/Projects/myproject_web/lib/python3.5/site-packages/django/db/models/sql/query.py" in build_lookup 1085. lookup = lookup_class(lhs, rhs)
File "/home/saleh/Projects/myproject_web/lib/python3.5/site-packages/django/db/models/lookups.py" in init 18. self.rhs = self.get_prep_lookup()
File "/home/saleh/Projects/myproject_web/lib/python3.5/site-packages/django/db/models/lookups.py" in get_prep_lookup 68. return self.lhs.output_field.get_prep_value(self.rhs)
File "/home/saleh/Projects/myproject_web/lib/python3.5/site-packages/django/db/models/fields/init.py" in get_prep_value 947. return int(value)
Exception Type: ValueError at /inbox/notifications/api/unread_list/ Exception Value: invalid literal for int() with base 10: ''
糟糕!这是我的错误。
我终于找出问题所在。 actor_object_id
是notifications_notification
table的字段,User.objects.get(username = 'SirSaleh')
保存在里面。应该是Interger
(演员的user_id
)。
所以我放弃了以前的 table 将实例更改为 User.objects.get(username = 'SirSaleh')
到用户 ID。问题已解决。
那么为什么 actor_object_id
的类型是 CharField (varchar)? (至少我不知道) ;))
actor_object_id 需要是 CharField 才能支持基于 UUID 的主键。
这是旧的,但我恰好知道答案。
在您的代码中,您写道:
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 并将其相应地存储在数据库中。