我需要为一组操作显示用户的Feed。我正在使用Django Activity Stream。我刚刚开始测试应用程序,但我似乎无法显示用户提要。
根据Action Stream documentation我正在使用actor_stream
函数,如下所示
from actstream.models import actor_stream
def user_feed(request):
print actor_stream(request.user)
这给了我一个错误
ValueError at /feed/
invalid literal for int() with base 10: '<property object at 0x2a6a520>'
这是追溯
Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
114. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/jaskaran/coding/buyingiq/authentication/feed/views.py" in user_feed
7. print actor_stream(request.user)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py" in __repr__
71. data = list(self[:REPR_OUTPUT_SIZE + 1])
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py" in __iter__
96. self._fetch_all()
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py" in _fetch_all
856. self._prefetch_related_objects()
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py" in _prefetch_related_objects
517. prefetch_related_objects(self._result_cache, self._prefetch_related_lookups)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py" in prefetch_related_objects
1598. obj_list, additional_prl = prefetch_one_level(obj_list, prefetcher, attr)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py" in prefetch_one_level
1697. prefetcher.get_prefetch_queryset(instances)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/contenttypes/generic.py" in get_prefetch_queryset
96. ret_val.extend(ct.get_all_objects_for_this_type(pk__in=fkeys))
File "/usr/local/lib/python2.7/dist-packages/django/contrib/contenttypes/models.py" in get_all_objects_for_this_type
174. return self.model_class()._base_manager.using(self._state.db).filter(**kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py" in filter
590. return self._filter_or_exclude(False, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py" in _filter_or_exclude
608. clone.query.add_q(Q(*args, **kwargs))
File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/query.py" in add_q
1198. clause = self._add_q(where_part, used_aliases)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/query.py" in _add_q
1234. current_negated=current_negated)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/query.py" in build_filter
1125. clause.add(constraint, AND)
File "/usr/local/lib/python2.7/dist-packages/django/utils/tree.py" in add
104. data = self._prepare_data(data)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/where.py" in _prepare_data
79. value = obj.prepare(lookup_type, value)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/where.py" in prepare
352. return self.field.get_prep_lookup(lookup_type, value)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py" in get_prep_lookup
371. return [self.get_prep_value(v) for v in value]
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py" in get_prep_value
613. return int(value)
Exception Type: ValueError at /feed/
Exception Value: invalid literal for int() with base 10: '<property object at 0x2a6a520>'
print type(actor_stream(request.user))
评估为<class 'actstream.gfk.GFKQuerySet'>
我甚至尝试将其打印为列表,然后打印它但它仍然会抛出相同的错误。我应该如何在视图中访问actor流?
编辑 - 如果有任何不同,我正在使用自定义用户模型。
编辑2 - 使用艾略特解决方案的回溯
Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
101. resolver_match = resolver.resolve(request.path_info)
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in resolve
318. for pattern in self.url_patterns:
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in url_patterns
346. patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in urlconf_module
341. self._urlconf_module = import_module(self.urlconf_name)
File "/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py" in import_module
40. __import__(name)
File "/home/jaskaran/coding/buyingiq/authentication/authentication/urls.py" in <module>
7. admin.autodiscover()
File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/__init__.py" in autodiscover
29. import_module('%s.admin' % app)
File "/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py" in import_module
40. __import__(name)
File "/usr/local/lib/python2.7/dist-packages/allauth/account/admin.py" in <module>
9. class EmailAddressAdmin(admin.ModelAdmin):
File "/usr/local/lib/python2.7/dist-packages/allauth/account/admin.py" in EmailAddressAdmin
16. 'last_name'])))
File "/usr/local/lib/python2.7/dist-packages/allauth/account/admin.py" in <lambda>
13. filter(lambda a: a and hasattr(User(), a),
File "/home/jaskaran/coding/buyingiq/authentication/users/models.py" in __init__
86. return self.pk
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py" in _get_pk_val
465. return getattr(self, meta.pk.attname)
Exception Type: AttributeError at /feed/
Exception Value: 'UserProfile' object has no attribute 'id'
答案 0 :(得分:0)
如果您这样做会怎样:
from actstream.models import actor_stream
def user_feed(request):
print actor_stream(request.user.pk) # added .pk here
正如您在stacktrace(return int(value)
)末尾所看到的,django尝试将request.user
转换为整数。我认为它失败了,因为您使用自定义用户模型,并且没有定义任何__int__
方法。尝试在用户模型中使用此方法:
类UserModel(models.Model):
# your logic here
def __int__(self):
return self.pk