简体   繁体   English

/ feed /处的ValueError-以10为底的int()的无效文字

[英]ValueError at /feed/ - invalid literal for int() with base 10

I need to display a feed for a user for a set of actions. 我需要为用户显示一组操作的供稿。 I'm using Django Activity Stream for this. 我为此使用Django活动流。 I just started to test the application but I can't seem to display the users feed. 我刚刚开始测试该应用程序,但似乎无法显示用户供稿。

As per the Action Stream documentation I'm using the actor_stream function as follows 根据动作流文档,我正在使用actor_stream函数,如下所示

from actstream.models import actor_stream

def user_feed(request):
    print actor_stream(request.user)

This gives me a error 这给我一个错误

ValueError at /feed/
invalid literal for int() with base 10: '<property object at 0x2a6a520>'

Here is the traceback 这是回溯

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)) evaluates to <class 'actstream.gfk.GFKQuerySet'> print type(actor_stream(request.user))计算为<class 'actstream.gfk.GFKQuerySet'>

I even tried typcasting it as a list and then printing it but it still throws the same error. 我什至尝试将它打成列表,然后打印出来,但仍然会引发相同的错误。 How should I be accessing the actor stream in my view? 我认为应该如何访问演员流?

EDIT - I'm using a Custom User Model if that makes any difference. 编辑 -如果有任何区别,我正在使用自定义用户模型。

EDIT 2 - Traceback for using Eliot's solution 编辑2-使用Eliot解决方案的回溯

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'

What if you do instead : 如果您改为这样做:

from actstream.models import actor_stream

def user_feed(request):
    print actor_stream(request.user.pk) # added .pk here

As you can see at the end of the stacktrace ( return int(value) ), django try to convert request.user to an integer. 如您所见,在stacktrace的结尾( return int(value) ),django尝试将request.user转换为整数。 I think it fails because you use a custom User model, and have not any __int__ method defined in it. 我认为它失败了,因为您使用了自定义的User模型,并且没有定义任何__int__方法。 Try to had this method in your User model : 尝试在用户模型中使用此方法:

class UserModel(models.Model): 类UserModel(models.Model):

# your logic here

def __int__(self):
    return self.pk

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM